[Twisted-Python] Lock class using Deferreds

Andrew Bennetts andrew-twisted at puzzling.org
Fri Mar 5 18:20:16 EST 2004


On Fri, Mar 05, 2004 at 11:19:01AM -0500, Itamar Shtull-Trauring wrote:
> class Lock:
>     """A lock for event driven systems."""

I have very very similiar looking code on my harddisk... what inspired you
to write this? :)

>     def release(self):
>         """Release the lock.
> 
>         Should be called by whoever did the acquire() when the shared
>         resource is free.
>         """
>         assert self.locked
>         self.locked = 0
>         if self.waiting:
>             # someone is waiting to acquire lock
>             self.locked = 1
>             d = self.waiting.pop(0)
>             d.callback(self)

In my version, I had a 'throttle' instance attribute:
    d = self.queue.pop(0)
    if self.throttle:
        reactor.callLater(0, d.callback, self)
    else:
        d.callback(self)

Considering I never actually *used* my version, this might be a premature
feature addition, though.

>     def _releaseAndReturn(self, r):
>         self.release()
>         return r

I wonder if this convenience function should also call log.err if
isinstance(r, failure.Failure)?  But that's probably just premature
featuritus again :)

So, where in Twisted is a good spot for it?  twisted.internet.util?

-Andrew.





More information about the Twisted-Python mailing list