[Twisted-Python] Abouth threads in twisted.
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? #====================================== from twisted.internet import reactor def notThreadSafe(x): """do something that isn't thread-safe""" # ... def threadSafeScheduler(): """Run in thread-safe manner.""" reactor.callFromThread(notThreadSafe, 3) # will run 'notThreadSafe(3)' in the event loop #====================================== 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.
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)".
Thanks for your reply! But I think I have to read some source code of twisted to make sure what was happening behind callFromThread. On Tue, Aug 19, 2008 at 11:58 PM, Itamar Shtull-Trauring < itamar@itamarst.org> wrote:
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)".
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
-- 科幻小说可能在哲学上是天真的,在道德上是简单的,在美学上是有些主观的,或粗糙的,但是就它最好的方面而言,它似乎触及了人类集体梦想的神经中枢,解放出我们人类这具机器中深藏的某些幻想。
participants (2)
-
Itamar Shtull-Trauring -
Peter Cai