[Twisted-Python] way of dealing with returning a deferred from a method/function

Cory Dodt moonfallen at twistedmatrix.com
Wed Dec 3 23:25:41 EST 2003


> def aLongWait(stuff):
>     d = defer.Deferred()
>     def _():
>          result = doSomeStuffHere(stuff)
>          d.callback(result)
>     reactor.callLater(0, _)
>     return d

What neither Bob nor Itamar have pointed out is *why* this doesn't work.

doSomeStuffHere() takes a long time to do its processing, and it blocks.
You've succeeded in making aLongWait not block its caller, but
doSomeStuffHere is still going to throw a monkey wrench into the works,
because it will still block later -- specifically, it blocks the reactor
loop.  When the reactor decides to run _(), everything else the reactor
is responsible for must stop and wait for doSomeStuffHere to finish.

To make it not block the reactor, you need to take one of their
suggestions: use a thread, or refactor the whole thing to do a little
bit at a time.  At some level these amount to the same thing, but using
a thread makes the OS responsible for deciding what constitutes "a
little bit", and the other way makes you responsible.  Each has
appropriate and inappropriate uses.

C




More information about the Twisted-Python mailing list