<div dir="ltr"><span style="font-family: courier new,monospace;">Hi All,<br><br>QUESTION 1 : <span style="color: rgb(255, 0, 0);">callFromThread<br><span style="color: rgb(0, 0, 0);">~~~~~~~~~~~~~~~~~~~~~~~~~~~</span><br></span><br>
I found this code snippet in twisted manual book. <br>But I don't quite get it.<br><br>What does "run 'notThreadSafe(3)' in the event loop" means?</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">Does the reactor promise that all those "notThreadSafe" functions<br>be called one by one so they won't interfere each other?</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"><br>#======================================<br><br><span style="color: rgb(255, 0, 0);">from twisted.internet import reactor</span></span><br style="font-family: courier new,monospace; color: rgb(255, 0, 0);">
<br style="font-family: courier new,monospace; color: rgb(255, 0, 0);"><span style="font-family: courier new,monospace; color: rgb(255, 0, 0);">def notThreadSafe(x):</span><br style="font-family: courier new,monospace; color: rgb(255, 0, 0);">
<span style="font-family: courier new,monospace; color: rgb(255, 0, 0);"> """do something that isn't thread-safe"""</span><br style="font-family: courier new,monospace; color: rgb(255, 0, 0);">
<span style="font-family: courier new,monospace; color: rgb(255, 0, 0);"> <span style="color: rgb(0, 102, 0);"># ...</span></span><br style="font-family: courier new,monospace; color: rgb(255, 0, 0);"><br style="font-family: courier new,monospace; color: rgb(255, 0, 0);">
<span style="font-family: courier new,monospace; color: rgb(255, 0, 0);">def threadSafeScheduler():</span><br style="font-family: courier new,monospace; color: rgb(255, 0, 0);"><span style="font-family: courier new,monospace; color: rgb(255, 0, 0);"> """Run in thread-safe manner."""</span><br style="font-family: courier new,monospace; color: rgb(255, 0, 0);">
<span style="font-family: courier new,monospace;"><br style="color: rgb(255, 0, 0);"><span style="color: rgb(255, 0, 0);">reactor.callFromThread(notThreadSafe, 3) <span style="color: rgb(0, 102, 0);"># will run 'notThreadSafe(3)' in the event loop</span></span><br style="color: rgb(255, 0, 0);">
<br>#======================================</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"><br>QUESTION 2 : Integrated with a threaded 3rd party library.<span style="color: rgb(255, 0, 0);"><br>
<span style="color: rgb(0, 0, 0);">~~~~~~~~~~~~~~~~~~~~~~~~~~~</span></span></span>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">I have a C library which using a work threaded pool model. It's interface is like this.<br>
<br> void runWorkAsyn(int args, void pcallbackfunc(int));<br><br>When runWorkAsyn is called, it dispatch work to a thread in its <br>own thread pool and call 'pcallbackfunc' in that thread when it's done.<br>
<br>I really got confused by how use this lib from twisted in a thread safe way.<br>If I use a python callback function as </span><span style="font-family: courier new,monospace;">pcallbackfunc and touch the reactor from it,<br>
it might damage the event look as most twisted APIs are not thread safe.<br></span></div>