[Twisted-Python] Synchronization techniques

Daniel Miller daniel at keystonewood.com
Tue Apr 3 15:07:34 MDT 2007


Thanks for the response Itamar,

>> 1. Is reactor.iterate() meant to be used this way (see
>> TwistedLock.acquire() and TwistedProcess.check_call())? If not, what
>> is the right way to do those things? I've read everything I can find
>> on reactor.iterate() and while it seems to be the only way to do what
>> I need to do, its use is often strongly discouraged. Why is it so
>> difficult for twisted to handle non-async code?
>
> You should never call reactor.iterate().

There really needs to be a way to do this:

d = getDeferred()
result = reactor.waitFor(d)

Or even better:

d = getDeferred()
result = d.waitFor()

The waitFor() method returns the result of the deferred or raises an  
exception if the deferred failed. This would allow a much more  
intuitive way to do the same type of thing that inlineCallbacks()  
allows. Why is this not implemented?

>
>> 2. Is my suspicion that the server could deadlock if I used
>> threading.Lock correct?
>
> Yes. And the Queue model uses threading.Lock, so using it can also
> deadlock you.

Right, but I'm using Queue.get_nowait() and Queue.put_nowait(),  
neither of which block, so that shouldn't be a problem.

>
>> 3. Is there a better way to do what I'm trying to do here? (getting a
>> new accounting system is not an option :)
>
> twisted.internet.defer.DeferredLock and some of the related classes  
> are
> what you ought to be using.

Unfortunately that only gets me half way there. DeferredLock.acquire 
() returns a deferred. How do I return the result of a deferred from  
a PB remote_xxx() function?

Thanks,
Daniel







More information about the Twisted-Python mailing list