[Twisted-Python] Abouth threads in twisted.

Itamar Shtull-Trauring itamar at itamarst.org
Tue Aug 19 11:58:56 EDT 2008

On Tue, 2008-08-19 at 20:08 +0800, Peter Cai wrote:
> Hi All,
> QUESTION 1 : callFromThread
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~
> I found this code snippet in twisted manual book.  
> But I don't quite get it.
> What does "run 'notThreadSafe(3)' in the event loop" means?
> Does the reactor promise that all those "notThreadSafe" functions
> be called one by one so they won't interfere each other?

They will be called in main thread, the one where you did reactor.run()

> QUESTION 2 : Integrated with a threaded 3rd party library.
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> I have a C library which using a work threaded pool model.  It's
> interface is like this.
>     void runWorkAsyn(int args, void pcallbackfunc(int));
> When runWorkAsyn is called, it dispatch work to a thread in its 
> own thread pool and call 'pcallbackfunc' in that thread when it's
> done.
> I really got confused by how use this lib from twisted in a thread
> safe way.
> If I use a python callback function as pcallbackfunc and touch the
> reactor from it,
> it might damage the event look as most twisted APIs are not thread
> safe.

Rather than passing in callable "doSomething" as callback, pass in:
"lambda *args: reactor.callFromThread(doSomething, *args)".

More information about the Twisted-Python mailing list