[Twisted-web] xmlrpc blocking

Mary Gardiner mary-twisted at puzzling.org
Mon Jul 25 02:46:31 MDT 2005

On Mon, Jul 25, 2005, Rasjid Wilcox wrote:
> What should I be doing instead?

Writing code that doesn't block.

A Deferred object is not a sophisticated wrapper which writes
asynchronous code for you, ie, wrapping a blocking function in a
Deferred does not make it non-blocking. (A Deferred is more like an
abstraction for chains of callback functions.) Wrapping a blocking
function in a Deferred just means that your application blocks for a
while, and *then* the Deferred fires the callbacks.

The use case for defer.succeed is actually for when you have a result
that is synchronous but want to pass it code that expects Deferreds. For
example, if I have:

def null():
    return 3

and want to use it like this:

d = null()

then I can wrap null using defer.succeed.

Your example doesn't say why it is that you need a blocking function.
The reason will affect the way you choose to make it non-blocking:

 1. you may interrupt your blocking function periodically and allow
    Twisted to return to the main loop, using twisted.flow or defgen
    (see http://mesozoic.geecs.org/cogito/archives/000160.html and one
    day I will write it up...).

 2. you may use an existing Twisted function that does an asynchronous
    version of whatever your blocking function is, or call some third
    party asynchronous library.

 3. You may need to use deferToThread in order to run the blocking
    function in a thread: this is usually the case if you're using some
    libraries that can't easily be made non-blocking (my major personal
    use case has been the PIL, there are plenty of others).


More information about the Twisted-web mailing list