[Twisted-Python] Twisted Advice

Andrew Francis andrewfr_ice at yahoo.com
Wed Apr 16 09:36:45 MDT 2008


Hi Paul:

>I would like to provide an environment where
>developers can write call servicing applications with
>no knowledge of the network. I had rested on a
>coroutine approach, whereby a developer could write
>something like this:

I posted some code a little while ago that has WS-BPEL
logic riding on top of Stackless and Twisted. 

>Am I correct in thinking that while a developer's
code
>is executing, all other connections are paused, and
>that the twisted server will not accept new
>connections until it returns?

[From the Schmitt Reactor Pattern Paper]

>Non-preemptive. In a single-threaded application
>process, event handlers are not preempted while they
>are executing. This implies that an event handler
>should not perform blocking I/O on an individual
>handle since this will block the entire process and
>impede the responsiveness for clients connected to
>other handles.

If you accept the Schmitt explanation, I would suggest
that it is convenient to think of Twisted as a
non-preemptive threading system where callbacks and
server protocol instances are user space "threads of
execution" and the reactor is the scheduler. Since a
"thread" cannot be involuntarily be pre-empted, the
reactor will not have a chance to trigger callbacks. 

Moral of the story - you don't want to do long CPU
intensive work in a callback.

>My original assumption was that Twisted would spawn a
>new thread within which the scheduler would be set to
>run to manage the communication for the duration of
>the customer call/interaction.

I think you want reactor.callInThread(). I remember
Philip Mayers telling me about this in a post about a
year ago.

Here is an example - forget the Stackless stuff

http://stacklessexamples.googlecode.com/svn/trunk/examples/twisted/TwistedWebserverThreaded.py

(Actually I would rather like to forget that piece of
code altogether)

However you may want to think over your design if you
find you need to extensively use OS threads. 

I would be tempted to ask myself questions like:

1)Is my application CPU or I/O bound"? 
2)Can I encapsulate the message exchange pattern
between the client and the server be done in a single
protocol"?
3)Can I conveniently yield the thread at predictable
points?

>Or, twisted is a framework whereby the response to a
>network event is expect to arrive immediately.

I think the exact opposite is true, that is why the
deferred object exists. 

I would suggest Twisted is ideal for reactive, I/O
bound applications particularly when you can represent
the message exchange pattern in a single protocol.

Cheers,
Andrew




      ____________________________________________________________________________________
Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ




More information about the Twisted-Python mailing list