Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subprocess.Popen pipeline example code in the documentation is lacking #51927

Closed
stevenkwong mannequin opened this issue Jan 11, 2010 · 4 comments
Closed

subprocess.Popen pipeline example code in the documentation is lacking #51927

stevenkwong mannequin opened this issue Jan 11, 2010 · 4 comments
Assignees
Labels
docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error

Comments

@stevenkwong
Copy link
Mannequin

stevenkwong mannequin commented Jan 11, 2010

BPO 7678
Nosy @gpshead, @merwok

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/gpshead'
closed_at = <Date 2011-02-05.21:49:27.952>
created_at = <Date 2010-01-11.21:26:43.924>
labels = ['type-bug', 'docs']
title = 'subprocess.Popen pipeline example code in the documentation is lacking'
updated_at = <Date 2011-02-05.21:49:27.951>
user = 'https://bugs.python.org/stevenkwong'

bugs.python.org fields:

activity = <Date 2011-02-05.21:49:27.951>
actor = 'gregory.p.smith'
assignee = 'gregory.p.smith'
closed = True
closed_date = <Date 2011-02-05.21:49:27.952>
closer = 'gregory.p.smith'
components = ['Documentation']
creation = <Date 2010-01-11.21:26:43.924>
creator = 'steven.k.wong'
dependencies = []
files = []
hgrepos = []
issue_num = 7678
keywords = []
message_count = 4.0
messages = ['97606', '124657', '127787', '128028']
nosy_count = 5.0
nosy_names = ['gregory.p.smith', 'eric.araujo', 'steven.k.wong', 'docs@python', 'rosslagerwall']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'needs patch'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue7678'
versions = ['Python 3.1', 'Python 2.7', 'Python 3.2']

@stevenkwong
Copy link
Mannequin Author

stevenkwong mannequin commented Jan 11, 2010

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)

@stevenkwong stevenkwong mannequin assigned birkenfeld Jan 11, 2010
@stevenkwong stevenkwong mannequin added the docs Documentation in the Doc dir label Jan 11, 2010
@BreamoreBoy BreamoreBoy mannequin assigned docspython and unassigned birkenfeld Aug 4, 2010
@merwok
Copy link
Member

merwok commented Dec 26, 2010

As a non-expert user of subprocess, calling close before communicate seems strange to me. Does the example code with a bug work if close is called after communicate?

In the 3.2 docs, we could update the example to use a with statement, unless it is deemed a distraction for this simple introductory section.

@merwok merwok added the type-bug An unexpected behavior, bug, or error label Dec 26, 2010
@rosslagerwall
Copy link
Mannequin

rosslagerwall mannequin commented Feb 3, 2011

The docs should be updated. This has been noted in msg54949 and http://www.enricozini.org/2009/debian/python-pipes/

Perhaps this example will make it clear:
import subprocess

p1 = subprocess.Popen(["yes"], stdout=subprocess.PIPE)
p2 = subprocess.Popen(["head"], stdin=p1.stdout, stdout=subprocess.PIPE)
#p1.stdout.close()
p1.wait()

This example hangs. "yes" writes to "head" and head reads the first 10 lines and then exits. But, "yes" does not receive a SIGPIPE because the python process still has a p1.stdout open. Thus, p1.stdout should be closed after being passed to p2.

@gpshead
Copy link
Member

gpshead commented Feb 5, 2011

documentation updated in r88352. thanks!

@gpshead gpshead closed this as completed Feb 5, 2011
@gpshead gpshead assigned gpshead and unassigned docspython Feb 5, 2011
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants