<div>I've figured the Windows open_port() delay problem and will document the issue here in case anyone else trips on it.</div><div><br></div><div>The function create_child_process() in erts/ermulator/sys/win32/sys.c makes the following call after the port process has been created:<div>
<br></div><div><div>WaitForInputIdle(piProcInfo.hProcess, 5000);</div><div><br></div><div>What this does is wait for the Windows message loop in the port process to become active.  If the port process doesn't start a message loop right away (or ever),  WaitForInputIdle(), and hence open_port() will just hang for up to 5 sec.</div>
<div><br></div><div>A partial work-around for this problem is to have the port program do a single iteration through the Windows message loop early in its life.  In my Python/PyQt port program this was achieved with:</div>
<div><br></div><div><div>app = QtGui.QApplication(sys.argv)</div><div>app.processEvents()</div></div><div><br></div><div><br></div><div>Apparently the purpose of the WaitForInputIdle() call was to address a memory leak problem in Windows NT 3.5 (<a href="http://support.microsoft.com/kb/124121">http://support.microsoft.com/kb/124121</a>) .  This has been fixed in NT 3.5 and everything following, so it is probably time to do away with the WaitForInputIdle() call so nobody else gets burnt by this issue.</div>
<div><br></div>Dan.</div><div><br><div class="gmail_quote">On Tue, Nov 8, 2011 at 4:19 PM, Daniel Goertzen <span dir="ltr"><<a href="mailto:daniel.goertzen@gmail.com">daniel.goertzen@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div>I am observing slow behavior for open_port() on Windows.  The port program itself starts up quickly, but open_port() itself hangs for several seconds before returning... long after the port program is up and running.</div>

<div><br></div><div><br></div><div>Here is my instrumented Erlang code....</div><div><br></div><div><div>  io:format("~p opening port~n",[now()]),</div><div>  Port=open_port( {spawn_executable, Exe},</div><div>
<span style="white-space:pre-wrap">             </span>    [</div>
<div><span style="white-space:pre-wrap">          </span>     {cd, Dir},</div><div><span style="white-space:pre-wrap">          </span>     {args, Args},</div><div><span style="white-space:pre-wrap">               </span>     stream,</div>
<div><span style="white-space:pre-wrap">          </span>     binary,</div><div><span style="white-space:pre-wrap">             </span>     exit_status</div><div><span style="white-space:pre-wrap">         </span>    ]),</div>
<div>  io:format("~p done port~n",[now()]),</div></div><div><br></div><div><br></div><div>My port program is a python PyQt program.  One of the first things I have it do is...</div><div><br></div><div>  logfile = open("c:\\logfile.txt","w")</div>

<div>  print(time.time(), "starting", file=logfile)</div><div> </div><div><br></div><div><br></div><div>The output of my Erlang code interspersed with the above starting message is....</div><div><br></div><div>
<div>
{1320,789477,830000} opening port    [werl.exe]</div><div>  1320789478.968 starting                 [my_port_program.exe]</div><div>{1320,789483,337000} done port        [werl.exe]</div></div><div><br></div><div><br></div>

<div><br></div><div>Any idea why open_port() waits for so long before returning?</div><div><br></div><div><br></div><div>Thanks,</div><div>Dan.</div>
</blockquote></div><br></div></div>