[Twisted-Python] Synchronization techniques

Andrew Francis andrewfr_ice at yahoo.com
Fri Apr 6 07:25:23 EDT 2007

Hello Brian:

I have just recently started reading this thread.

>1.  The interactive python interpreter is a
completely >synchronous universe - getting the reactor
running in >this context seems like a hack.  The only
way I have >seen this done is by running the reactor
>a different thread.  The problem with this is that it
>is inevitable that you end up wanting to do things
>with Deferreds in the main thread where user code is
>running.  But, as I understand it, Twisted is not
>thread safe, so at that point, you are playing with
>(threaded) fire.

I recented subscribed to this mailing list to ask
questions about integrating Stackless with Twisted....

I execute Stackless and Twisted in their own threads.
Otherwise when the Twisted reactor blocks, Stackless
tasklets (read threads) that could otherwise run,
would be blocked too.

I start the Stackless thread with a
reactor.callInThread().  Because of thread safety, I
use the callFromThread() method to make Twisted calls.
Also for safety and performance, I make the threads
communicate through a deque (I took Phil Mayer's

As for deferreds. Essentially I make the deferreds and
Stackless threads communicate through channels. So
Stackless tasklets block until there is input. 

There are still a few issues and I am still conducting
tests, However things seem to be working...

One of the Stackless members, Carlos de Paula has
started a repository that includes Twisted/Stackless
examples. There are examples using the Perspective
Broker and callInThread/callFromThread.



No need to miss a message. Get email on-the-go 
with Yahoo! Mail for Mobile. Get started.

More information about the Twisted-Python mailing list