[Twisted-web] Are twisted xmlrpc calls non-blocking?

Jean-Paul Calderone exarkun at divmod.com
Thu Feb 8 08:58:35 CST 2007

On Thu, 08 Feb 2007 14:45:20 +0100, Remi Cool <mailinglists at smartology.nl> wrote:
>Jean-Paul Calderone wrote:
>> On Thu, 08 Feb 2007 13:31:27 +0100, Remi Cool
>> <mailinglists at smartology.nl> wrote:
> [snip]
>>> I tend to think not, because when I call a method with a 5 sec delay
>>> from a "runInteraction" thread, the thread returns when it handled the
>>> call. This is how I prefer it in this case, but when I want to use the
>>> xmlrpcCall method from a non "runInteraction" call, do I need to wrap it
>>> up in a thread myself?
>> Oops!  You _can't_ do that.  Twisted APIs must be invoked from the
>> reactor
>> thread.  They don't work in any other thread.
>Let me explain:
>a user calls the xml-rpc method "init" on my twisted xmlrpc server like:
>    """"""
>    return self.service.connPool.runInteraction(myInit, self.service)
>The myInit function has to fetch some data from the database and from a
>remote xml-rpc server.
>So ... first I get the data from the database , print a log line ...
>call the service.xmlrpcCall method ... and print another log line.
>The method on the remote xml-rpc server delays 5 secs, prints a log line
>and then sends back a string.
>The log lines are in the right order.  But you say the xmlrcpCall method
>doesn't block?

It doesn't block, but it doesn't work very well, since it uses Twisted APIs
from a non-reactor thread.  The 5 seconds are probably just spent idling,
and then some unrelated event wakes up the reactor, at which point it then
notices that you asked it to do something, and does it.

>So if I understand correctly, I can't use any twisted api's in threads,
>even if they are created with threads.deferToThread?

Correct.  You can call back into the reactor thread, though.  See the
callFromThread method of the reactor.


More information about the Twisted-web mailing list