[Twisted-Python] DeferredSemaphore - action on acquire and release

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Tue Feb 8 06:35:03 MST 2011


On 01:14 pm, jrennie at gmail.com wrote:
>Sorry, I don't think I understand the example either---looks to me like
>you're reimplementing a semaphore.  Stephen's right about run().

It sounds like (the other) Jason would be happy with run, except he also 
wants to do something extra each time the lock/semaphore is taken and 
released.  DeferredSemaphore (and the other similar classes) don't 
really offer this functionality.  You just have to work that logic into 
the function you run.  It's possible something like this would be nicer 
than reimplementing the acquire/release logic:

    def run(sem, f):
        def before():
            doAcquire()
            return f()
        d = sem.run(before)
        def after(passthrough):
            doRelease()
            return passthrough
        d.addBoth(after)
        return d

Jean-Paul




More information about the Twisted-Python mailing list