Reputation: 1043
Following code finishes correctly,
import subprocess
p = subprocess.Popen("cat", stdin=subprocess.PIPE)
p.stdin.close()
p.wait()
print p.returncode
but following code never end.
import subprocess
p1 = subprocess.Popen("cat", stdin=subprocess.PIPE)
p2 = subprocess.Popen("cat", stdin=subprocess.PIPE)
p1.stdin.close()
p1.wait()
p2.stdin.close()
p2.wait()
print p1.returncode, p2.returncode
The doc says,
Warning This will deadlock if the child process generates enough output to a stdout or stderr pipe such that it blocks waiting for the OS pipe buffer to accept more data. Use communicate() to avoid that.
but in this case, no output is generated. Why it deadlocks?
I'm using Linux, and tried both python2.5 and 2.6.
--
edit
I also tried python2.7.1 and 3.2.3 on MacOSX. The results as follows
Is this a bug in older python? Is there any workaround?
Upvotes: 1
Views: 731
Reputation: 1043
Finnaly, I noticed the answer by myself.
close_fds=True
is needed to call Popen().
Because Popen does pipe(2)
and fork(2)
, not only parent process but also child process must close output side of the pipe.
On only newer python, close_fds=True seems to be default.
Thanks anyway ;)
Upvotes: 2