This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author steven.k.wong
Recipients georg.brandl, steven.k.wong
Date 2010-01-11.21:26:43
SpamBayes Score 7.862321e-09
Marked as misclassified No
Message-id <1263245210.2.0.116487607425.issue7678@psf.upfronthosting.co.za>
In-reply-to
Content
The example code at http://www.python.org/doc/2.6.2/library/subprocess.html#replacing-shell-pipeline should be updated to add the close() call noted below:

output=`dmesg | grep hda`
==>
p1 = Popen(["dmesg"], stdout=PIPE)
p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close()  # <----- this line is not in the doc, but we should add it
output = p2.communicate()[0]

The parent process does not use p1.stdout after passing it to p2, so it should close p1.stdout after that. If it is not closed, there is a possibility for p1's process to never terminate if p2's process fails. The following code demonstrate this situation:

from subprocess import Popen, PIPE
import time
# p1 is a program that writes lots of data to the pipe
cmd = ['cat', 'a_large_file']
p1 = Popen(cmd, stdout=PIPE)
# p2 is a program that fails without reading much data from the pipe
cmd = ['python', '-c', 'import time; time.sleep(5); asdf']
p2 = Popen(cmd, stdin=p1.stdout, stdout=PIPE)
output = p2.communicate()[0]
while p1.poll() is None:  # it loops forever
    print 'sleep a bit'
    time.sleep(1)
History
Date User Action Args
2010-01-11 21:26:50steven.k.wongsetrecipients: + steven.k.wong, georg.brandl
2010-01-11 21:26:50steven.k.wongsetmessageid: <1263245210.2.0.116487607425.issue7678@psf.upfronthosting.co.za>
2010-01-11 21:26:43steven.k.wonglinkissue7678 messages
2010-01-11 21:26:43steven.k.wongcreate