[Twisted-Python] Synchronous calls using Twisted?

Andrew Francis andrewfr_ice at yahoo.com
Thu May 17 13:06:04 MDT 2012

Message: 1

Date: Thu, 17 May 2012 00:58:57 +0200
From: Louis <spalax at gresille.org>
Subject: [Twisted-Python] Synchronous calls using Twisted?
To: twisted-python at twistedmatrix.com
Message-ID: <4FB43131.2030805 at gresille.org>
Content-Type: text/plain; charset=ISO-8859-1

>  Hello,
>  I am writing an application, which architecture is (I hope my
>beautiful drawing is not messed up):

>  +-------------+   +------------+   +-----------+
>  | Core Server |---| Web Server |---| Web client|
>  +-------------+   +------------+   +-----------+

>  Do you think Twisted is the right tool for my use case, or am I doing
>an ugly hack to do what I want, which means I should rather use another

I am not sure why inline generators won't work?

Over the years, I use Stackless Python with Twisted. I use Twisted for many of the reasons you describe. For myself there is another dimension: I find that a lot of the code I write are orchestrations - that is in order to compute something, I have to make a few network calls sequentially. 

 I use a technique that Christopher Armstrong (what happened to him?) called a blockOn. For the sake of being academic (roll your eyes here),  this is an example of an obscure design pattern called "Half-sync/Half-async."

Essentially one does the following:

def blockOn(deferred): 
    ch = stackless.channel() 
    def cb(result): 
    return ch.receive() 

Of course, there is a bit more, like the Twisted reactor is running in its own tasklet. But this has the effect of nicely allowing one to use Twisted in a synchronous fashion. If you don't feel like installing Stackless Python, then you can use stackless.py (not the new version that uses continuations) with the greenlet package.

Here is a link to a complete example


-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20120517/75930e2a/attachment.html>

More information about the Twisted-Python mailing list