<div>I am&nbsp;attempting to add&nbsp;spawnProcess to iocpreactor.&nbsp; In order to begin this task I've had to do a lot of reading on Windows network programming, specifically the various Windows I/O methods, to attempt to understand what win32eventreactor and iocpreactor are doing, and also just increase my understanding of how reactors work in general.&nbsp; To understand the various Winsock 2 methods that both of these reactors rely upon, I read chapters 1-5 of Network Programming for Microsoft Windows[1].
</div>
<div>&nbsp;</div>
<div>Before actually attempting to add spawnProcess, I would like to present how I think iocpreactor works&nbsp;and how I think I should add spawnProcess, and hopefully be corrected or confirmed in my understanding.&nbsp; If I'm too vague&nbsp;there's a good chance it's because I don't understand it very well.&nbsp; Please feel free to point out things that you might think are obvious but aren't sure I understand.
</div>
<div>&nbsp;</div>
<div>How iocpreactor works</div>
<div>---------------------------------</div>
<ol>
<li>Create an IO Completion Port.</li>
<li>Create a socket and associate it with the IOCP.&nbsp; This is the socket we will call AcceptEx (a non-blocking accept)&nbsp;on.&nbsp; The association with the IOCP is made via CreateIoCompletionPort.</li>
<li>Setup any scheduled tasks on the reactor.</li>
<li>Call AcceptEx (which doesn't block)&nbsp;on the socket.&nbsp; AcceptEx takes an overlapped structure as a parameter.&nbsp; Before making the call, we set two attributes of the struct: the callback and callback_args which will be called when an accept event completes on the socket.&nbsp; The Winsock 2 methods don't actually call the callback.&nbsp; The Winsock 2 methods handle copying data related to the network event that occurred on the socket into the overlapped structure and making that overlapped structure available to GetQueuedCompletionStatus.&nbsp; So when we handle events on sockets via GetQueuedCompletionStatus from within doIteration, we have access to the data related to the event as well as the callback and callback_args we call to handle that event.&nbsp; The callbacks are setup in the xxxOp classes in 
ops.py and always result in some transport method getting called (such as readDone, connectionDone, etc).</li>
<li>From within doIteration, call GetQueuedCompletionStatus (which does block)&nbsp;with a timeout of the time until&nbsp;the next scheduled task needs to be run.&nbsp; If any event&nbsp;occurs on the sockets currently associated with the IOCP before that time expires,&nbsp;GetQueuedCompletionStatus will return (stop blocking).&nbsp; Now we have access to the overlapped structure containing data associated with the event which was copied into the overlapped structure's buffer, such as data received from WSARecv calls, as well as the callback and callback_args.&nbsp; From within doIteration we call the callbacks passing in the data related to the event.&nbsp; Depending on the events we are handling, we may create new sockets (
e.g. end point sockets&nbsp;in TCP connections) and associate them with the IOCP as well.&nbsp; All Winsock 2 API calls made are non-blocking accept for GetQueuedCompletionStatus.</li>
<li>Step 5 continues until the reactor stops.</li></ol>
<div>&nbsp;</div>
<div>&nbsp;</div>
<div>How to add spawnProcess</div>
<div>---------------------------------------</div>
<ol>
<li>Create the processes via&nbsp;Windows APIs&nbsp;and associate their stdout/err with with the IOCP via CreateIoCompletionPort calls.</li>
<li>Close stdin.</li>
<li>Notify the ProcessProtocol via protocol.makeConnection (not sure why, looking at win32eventreactor)</li>
<li>Receive data from stdout/err via the completion port by calling GetQueuedCompletionStatus from within doIteration.&nbsp; Is this really possible?&nbsp; ProcessProtocol's methods won't get called appropriately by letting the existing callbacks in 
ops.py make calls to the transport (e.g. connectionDone, readDone)?</li></ol>
<div>&nbsp;</div>
<div>&nbsp;</div>
<div>Thanks for your help.</div>
<div>Justin</div>
<div>&nbsp;</div>
<div>[1] <a href="http://www.amazon.com/exec/obidos/ASIN/0735615799">http://www.amazon.com/exec/obidos/ASIN/0735615799</a></div>