[Twisted-Python] How to force synchronous behavior

Moe Aboulkheir moe at divmod.com
Sat Oct 29 19:33:16 EDT 2005

On Sun, 30 Oct 2005 00:11:07 +0200 (SAST), Clive Crous <clive at darkarts.co.za> wrote:

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

no, it really doesn't.  here is the code snippet given by the original poster:

def mySyncFunc()
   x = 0
   def done(data):
      global x
      x = data

   d = someCalculation()
   <something here to hold until "done" is really done>
   return x

the desired functionality is "at the end i print the result of someCalculation() and not just 0".  this could obviously be done by changing the first line of done() to "print data".  but the "return x" betrays the intention that the function would ideally return the value of "x" (returning the _value_ of "x" is worlds away from returning a deferred that fires with the value of "x").  no defgen type solution is going to make this possible

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

the point here is that this particular question does not have a satisfying answer.  it has been beaten to death recently that you cannot magically expose a synchronous interface to asynchronous code.  so, in summary, the question was answered, by several people in this thread, and the answer is "you cannot do that and expect your code to work reliably".  is that not a valid answer?

>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 ?

ideally yes.  somebody unfamiliar with deferreds is going to be in no position to determine whether or not they are comfortable with a generator based mechanism for handling them.  people already associate unfair amounts of magic with twisted, and i dont think suggesting such abstract interfaces to beginners is an effective way to mitigate this.

Moe Aboulkheir

More information about the Twisted-Python mailing list