[Twisted-Python] How to force synchronous behavior

Clive Crous clive at darkarts.co.za
Sat Oct 29 18:11:07 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

The origional question was:
""" So, how can I write mySyncFunc() so that at the end I print the result
of someCalculation() and not just 0? """

My example and proposed solution does that. And as such is usable by him.

# 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())

it is an example usage, not a real world implementation, of course if
something fails it will die.

# 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.

Why do i hit this wall constantly when discussing twisted usage with
twisted users or developers mostly on freenode's #twisted admitedly:
Telling someone NOT to do something is not answering a question it is
avoiding it.

I pasted the url for the full source so that he could "look under the
hood" if he feels uncomfortable with it's mechanism he needent use it.  By
your reasoning surely he should go through the entire twisted codebase
aswell and know exactly what a deffered is doing "under the hood" and how
twisted does it's asyncronousity before using it ?

Clive Crous

More information about the Twisted-Python mailing list