[Twisted-Python] reactor.run() do not return after reactor.stop()

Glyph Lefkowitz glyph at twistedmatrix.com
Sat Oct 10 15:47:00 EDT 2009


On Sat, Oct 10, 2009 at 12:50 PM, Stanislav Yudin <decvar at gmail.com> wrote:

> Thanks, but how can I pass control back to calling code after starting the
> reactor? Or the idea is to run it in separate thread? I understand that main
> idea on twisted is async processing, but this time I need to block the
> calling code until response is received.


The way that you want to do this is to run all of your Twisted code in a
thread, and make calls into it from a different thread using one of the
utility APIs for that purpose.  See, for example, <
http://twistedmatrix.com/documents/8.2.0/api/twisted.internet.threads.html#blockingCallFromThread>.
 Remember not to touch any Twisted APIs directly from the non-Twisted
thread.  Leave the Twisted I/O thread running for the entire lifetime of
your process; don't start and stop it repeatedly.

For what it's worth, you should really look into restructuring your
application so that this kind of hack is not necessary.  It will almost
certainly have hard-to-debug unforseen consequences.  There's nothing
fundamentally broken here, but it is much harder to keep track of and get
right than you think it will be, just based on the fact that many many
people who try this strategy end up giving up on it.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20091010/f73ec020/attachment.htm 


More information about the Twisted-Python mailing list