<div class="gmail_quote">On 7 June 2012 17:04, Julio Sergio <span dir="ltr"><<a href="mailto:juliosergio@gmail.com" target="_blank">juliosergio@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I'm trying to call an external process to filter some of my data, i.e., I'm<br>
trying to pass some information to the called process, and have this information<br>
back transformed. I started testing with the linux 'cat' command, in this way:<br>
<br>
<br>
->>> import subprocess as sp<br>
->>> p = sp.Popen(["cat"],stdin=sp.PIPE,stdout=sp.PIPE,close_fds=True)<br>
->>> (fi,fo) = (p.stdin, p.stdout)<br>
->>> fi.write("SOMETHING\n")<br>
->>> fi.flush()<br>
->>> fo.readline()<br>
'SOMETHING\n'<br>
->>> fi.write("OTHER\n")<br>
->>> fi.flush()<br>
->>> fo.readline()<br>
'OTHER\n'<br>
->>> fi.write("NEXT\n")<br>
->>> fi.flush()<br>
->>> fo.readline()<br>
'NEXT\n'<br>
->>> fi.write("NEXT1\n")<br>
->>> fi.flush()<br>
->>> s = fo.readline()<br>
->>> s<br>
'NEXT1\n'<br>
<br>
Up to this point it worked as expected. However, when I tryied with the methods<br>
that write and read several lines, apparently the process got stalled:<br>
<br>
->>> fi.writelines(["uno\n","dos\n","tres\n"])<br>
</blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">->>> fi.flush()<br></blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
->>> s = fo.readlines()<br></blockquote><div><br></div><div>The readlines() method is intended to read an entire file and split it into lines, so it blocks until EOF is found. If you want to use readlines(), you should first close the subprocesses stdin:</div>
<div><div> </div><div>>>> fi.writelines(["uno\n","dos\n","tres\n"])</div><div>>>> fi.flush()</div><div>>>> fi.close()</div><div>>>> s = fo.readlines()</div>
</div><div><br></div><div>Although now you can no longer use the subprocess for reading or writing. If that is not what you want in your actual problem then you'll need to avoid readlines().</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

.<br>
.<br>
.<br>
<br>
Do you have any comments on this?<br>
<br>
Thanks,<br>
<br>
 --Sergio<br>
<span class="HOEnZb"><font color="#888888"><br>
<br>
--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></span></blockquote></div><br>