<div>When I try to run it, I get a hang too, in my case (there may be other factors for you) what's happening is because of the openpty stuff.</div><div><br></div><div>In the parent process, you're creating two file handles, master and slave.  Subprocess then forks, meaning the child inherits them.  At this point, both the parent and child processes have the same PTY handles open.</div><div><br></div><div>In my version of the code, there's a traceback in uniq.py that's preventing making the child bail out early.  At this point the child's FDs are closed, but because the master still has the 'slave' FD open, the pty isn't being shut down.</div><div><br></div><div>Add an os.close() after the subprocess call:</div><div><br></div><div><div>process = Popen("python -u uniq.py", shell=True, stdin=PIPE, stdout=slave)</div><div>    os.close(slave)</div></div><div><br></div><div>This will cause the pty to collapse when the child exits, which should stop the hanging read().</div><div><br></div><div>After adding some UTF-8 decoding to the uniq.py file, I managed to get this:</div><div><br></div><div><div>>       assert expected == result</div><div>E       assert ['bar\n', 'fo...o\n', 'end\n'] == ['bar\r\n', 'f...n', 'end\r\n']</div><div>E         At index 0 diff: 'bar\n' != 'bar\r\n'</div><div>E         Full diff:</div><div>E         - ['bar\n', 'foo\n', 'bar\n', 'foo\n', 'end\n']</div><div>E         + ['bar\r\n', 'foo\r\n', 'bar\r\n', 'foo\r\n', 'end\r\n']</div><div>E         ?       ++         ++         ++         ++         ++</div></div><div><br></div><div>Which seems quite close :)</div><div><br></div><div>Steve</div><div><br></div><br><div class="gmail_quote">On Wed Jan 07 2015 at 10:10:45 PM Tom Viner <<a href="mailto:tom@viner.tv" target="_blank">tom@viner.tv</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi all,<div><br></div><div>So before the London dojo meets again tomorrow night I wanted to resolve a question from last month.</div><div><br></div><div>The task last month was to make our own GNU uniq commands. After the dojo I got our team's code working with the <a href="https://docs.python.org/2/library/fileinput.html" target="_blank">fileinput module</a>. This allowed the flexibility of seamlessly reading from either stdin or taking filenames as arguments.</div><div><br></div><div>With fileinput <a href="https://github.com/tomviner/pydojo-uniq-s6e4/blob/master/team1/uniq.py#L11" target="_blank">the code</a> can just say:</div><div><span style="color:rgb(167,29,93);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;line-height:11.1999998092651px;white-space:pre-wrap">for</span><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;line-height:11.1999998092651px;white-space:pre-wrap;background-color:rgb(248,238,199)"> line </span><span style="color:rgb(167,29,93);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;line-height:11.1999998092651px;white-space:pre-wrap">in</span><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;line-height:11.1999998092651px;white-space:pre-wrap;background-color:rgb(248,238,199)"> fileinput.input():</span><br></div><div><br></div><div>and that gives you all these usages:</div><div><br></div><div>python uniq.py my_filename.txt other_file.txt<br></div><div>echo -e "hello\nhello2" | python uniq.py<br></div><div>python uniq.py <(python print_sleep_print.py)<br></div><div><br></div><div>With the <a href="https://github.com/tomviner/pydojo-uniq-s6e4/blob/master/team1/print_sleep_print.py" target="_blank">print_sleep_print.py script</a> I can even see about 8Kb at a time will be buffered into uniq.py, made unique and printed, and then more data will be passed in.</div><div><br></div><div>The problem came when testing this feature. I found a way to connect to both the input and output of a subprocess running the command:</div><div><br></div><div><div><font face="monospace, monospace">    master, slave = pty.openpty()</font></div><div><font face="monospace, monospace">    process = Popen("python uniq.py", shell=True, stdin=PIPE, stdout=slave)</font></div><div><font face="monospace, monospace">    stdin_handle = process.stdin</font></div><div><font face="monospace, monospace">    stdout_handle = os.fdopen(master)</font></div></div><div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="arial, helvetica, sans-serif">I then write some data in, and read data out. It all works except for one thing: even if I close the stdin_handle the stdout_handle will just block once I've read all the output. <a href="https://github.com/tomviner/pydojo-uniq-s6e4/blob/master/team1/test_uniq.py#L37" target="_blank">Full test here</a>.</font><br></div><div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="arial, helvetica, sans-serif">Appreciate any insight!</font></div><div><font face="arial, helvetica, sans-serif">Tom</font></div></div>
______________________________<u></u><u></u>_________________<br>
python-uk mailing list<br>
<a href="mailto:python-uk@python.org" target="_blank">python-uk@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-uk" target="_blank">https://mail.python.org/<u></u>mailma<u></u>n/listinfo/python-uk</a><br>
</blockquote></div>