<div>Great explanation. Thanks!</div><div><br></div><div>Jason<br><br><div class="gmail_quote">On Wed, Jan 12, 2011 at 4:40 PM, <span dir="ltr"><<a href="mailto:exarkun@twistedmatrix.com" target="_blank">exarkun@twistedmatrix.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div></div><div>Some reactors have a different implementation of wakeUp than the above,</div></div>
but they all have the same goal.<br>
<br>
The reactor thread may be doing something which is going to block<br>
indefinitely (WaitForMultipleObjects, select(), epoll_wait(), etc). The<br>
purpose of the wakeUp call is to cause that blocking call to end. Once<br>
it ends, calls in the threadCallQueue can be processed.<br>
<br>
The reactor is written such that if you are running code in the same<br>
thread it is running in (ie, you are "in the reactor thread") then any<br>
event source you create (be it network or time or whatever), the reactor<br>
will be sure to service it in a timely manner. For network event<br>
sources, this means it will include a descriptor in the select() (etc)<br>
call. For timing event sources (ie callLater), it means the reactor<br>
will set a timeout on the select() (etc) call so that it returns before<br>
it is time for that delayed call to execute.<br>
<br>
However, if you are not in the reactor thread, then really the only<br>
thing you're allowed to do to Twisted is use reactor.callFromThread.<br>
Since this might happen at any time with respect to what the reactor<br>
thread is doing, there's no way to be sure a (let's call it a) thread<br>
call event will get handled in a timely fashion. So this is the problem<br>
that wakeUp solves. After the thread call event is added to<br>
threadCallQueue, wakeUp makes some event source the reactor is<br>
monitoring signal readiness (on POSIX, it writes a byte to a pipe the<br>
reactor is select()ing (etc) on; on Windows, it posts an event like you<br>
pasted above). This ensures that if the reactor is asleep waiting for<br>
an event it will wake up soon, and then it will notice there are thread<br>
call events to process.<br>
<div><div></div><div><br>
Jean-Paul<br>
<br>
_______________________________________________<br>
Twisted-Python mailing list<br>
<a href="mailto:Twisted-Python@twistedmatrix.com" target="_blank">Twisted-Python@twistedmatrix.com</a><br>
<a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>Jason Rennie<br>Research Scientist, ITA Software<br>617-714-2645<br><a href="http://www.itasoftware.com/" target="_blank">http://www.itasoftware.com/</a><br><br>
</div>