<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.&nbsp; <br>But I don&#39;t quite get it.<br><br>What does &quot;run 'notThreadSafe(3)' in the event loop&quot; 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 &quot;notThreadSafe&quot; functions<br>be called one by one so they won&#39;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);">&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;do something that isn't thread-safe&quot;&quot;&quot;</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);">&nbsp;&nbsp;&nbsp; <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);">&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;Run in thread-safe manner.&quot;&quot;&quot;</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.&nbsp; It&#39;s interface is like this.<br>
<br>&nbsp;&nbsp;&nbsp; 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 &#39;pcallbackfunc&#39; in that thread when it&#39;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>