[Twisted-web] Reactor in a multithreaded program

Andrew Bennetts andrew-twisted at puzzling.org
Mon May 23 00:38:16 MDT 2005


On Sun, May 22, 2005 at 11:20:05PM -0700, Ellers wrote:
[...]
> > Nothing in Twisted is thread-safe, except for reactor.callFromThread.  So,
> > if you want to stop the reactor from thread aside from the one the reactor
> > is in, you need to do reactor.callFromThread(reactor.stop).
> 
> Ah - that makes sense, thanks :)
> 
> I tried that, but in itself it didn't solve my problem.
> 
> Would you expect the stop() message only to be picked up when the reactor
> has some activity?

No, the callFromThread will wake up the reactor if it is idle.

Are you sure reactor.callFromThread(reactor.stop) is being called?  And make
sure you aren't accidentally doing "reactor.callFromThread(reactor.stop())",
which is a common mistake!

> Is there an advised pattern for working with the reactor with multiple
> threads?

Yes: use callFromThread everytime you need to call a Twisted function from a
thread :)

There's a brief document on Twisted's threading facilities here:
   http://twistedmatrix.com/projects/core/documentation/howto/threading.html

It's brief because there's not much Twisted-specific knowledge to know,
beyond the advice about using callFromThread, and the APIs for using the
threadpool facilities built into Twisted.

-Andrew.




More information about the Twisted-web mailing list