[Twisted-Python] How to force synchronous behavior

Moe Aboulkheir moe at divmod.com
Sat Oct 29 17:47:02 EDT 2005

On Sat, 29 Oct 2005 22:12:26 +0200 (SAST), Clive Crous <clive at darkarts.co.za> wrote:
>#>> I want to encapsulate deferreds into a function that looks like
>#>> synchronous when invoked.
>I use a similar mechanism in my database wrapper module.
>here is an example of it in use:

the OP wanted a function that looks synchronous when *invoked* - the example you gave doesn't look synchronous to the caller at all - the functions decorated with "genAside" return deferreds.  it's easy to pretend that they don't, if you decorate all of your functions similarly, but at some point some plain, upstream  function is going to have to deal directly with a deferred (this is not the case with the example because it is a script, and nothing actually depends on the result of doit()).  also, if any operation inside the doit() function raises an exception, the reactor will not stop.  something like this would probably be better:

doit().addErrback(log.err).addCallback(lambda ignore: reactor.stop())

i'd like to reiterate glyph's advice that suggesting generator based short-cuts to twisted beginners is not a good idea.  it's *very* easy to use these things without understanding what is actually going on under the hood, and massive confusion frequently arises as soon as something crops up that doesn't fit into that programming style. 

Moe Aboulkheir

More information about the Twisted-Python mailing list