[Twisted-Python] Really Basic clarification on defers

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Wed Aug 5 09:17:53 MDT 2009


On 09:33 am, johnaherne at rocs.co.uk wrote:
>On Wed, Aug 5, 2009 at 12:14 AM, Johann Borck 
><johann.borck at densedata.com>wrote:
>>
>[snip]
>
>Sendline is not blocking so as you say we can avoid the use of 
>deferreds and
>continue to use sendline directly.

LineReceiver.sendLine is not blocking, correct.  However, your statement
implies that if it were blocking, you could use Deferreds to address 
this
problem.  This is incorrect.  Deferreds do not make blocking APIs into
non-blocking APIs.
>[snip]
>
>Our option 3 using defertothread does use sendline from the thread. 
>Your
>response implies that is OK since you say defertothread is threadsafe. 
>Did
>you really mean that.

deferToThread is not thread-safe: you may only call it from the reactor 
thread
(the thread in which you called reactor.run).

Since deferToThread runs the function you pass to it in a non-reactor 
thread,
you may not use any non-thread-safe Twisted APIs in the function you 
pass to
it.
>
>Once again thanks for a very good response. That has cleared up a lot 
>of
>confusion.
>
>I suppose it would help if there was a paragraph at the start of the 
>twisted
>documentation detailing what you have just said. So when they start on
>deferreds you have some sort of context in which to interpret what is 
>being
>said

A significant effort is presently underway to improve the documentation 
about
Deferreds.  Any specific feedback you have about it would be much 
appreciated.
:)

Jean-Paul




More information about the Twisted-Python mailing list