[Twisted-Python] Considering Twisted for OfflineIMAP

Matthias Urlichs smurf at smurf.noris.de
Fri Aug 1 06:36:23 EDT 2003


Hi, Tommi Virtanen wrote:

> counting the currently open IMAP connections and when the number
> 	is below the limit, taking more work from a queue

Or you could use a semaphore.

Feel free to add this code to twisted.internet.deferred.
The interface should be identical to python.threading.Semaphore,
except that I was too lazy to add any verbose statements...

class Semaphore(object):
    def __init__(self, value=1, verbose=None):
        self.queue=[]
        self.value=value

    def acquire(self):
        d=Deferred()
        if self.value:
            self.value -= 1
            d.callback(False)
        else:
            self.queue.append(d)
        return d

    def release(self):
        if self.queue:
            self.queue.pop(0).callback(True)
        else:
            self.value += 1

The callback's parameter answers the "did I have to wait for a slot"
question, in case the caller needs that question answered. (Most don't.)

-- 
Matthias Urlichs   |   {M:U} IT Design @ m-u-it.de   |  smurf at smurf.noris.de
Disclaimer: The quote was selected randomly. Really. | http://smurf.noris.de
-- 
s = (char*)(long)retval;                /* ouch */
             -- Larry Wall in doio.c from the perl source code





More information about the Twisted-Python mailing list