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.

classification
Title: "for line in file" is *still* broken in Python 2.7 on pipes
Type: behavior Stage: resolved
Components: IO Versions: Python 2.7
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: Andy.Lutomirski, ned.deily
Priority: normal Keywords:

Created on 2012-08-01 21:56 by Andy.Lutomirski, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (2)
msg167170 - (view) Author: Andy Lutomirski (Andy.Lutomirski) Date: 2012-08-01 21:56
This program:

import subprocess, sys
p = subprocess.Popen(['bash', '-c', 'while true; do echo x; sleep 1; done'], bufsize=0, stdout=subprocess.PIPE)

for line in p.stdout:
    sys.stdout.buffer.write(line)
    sys.stdout.flush()

sits around and does nothing on Python 2.7.3.  It works (i.e. prints 'x' once per second) on Python 3.

This was http://bugs.python.org/issue3907 and is supposedly fixed, but it's not.
msg167189 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2012-08-02 05:04
Notice in the reply to Issue3907, "with 2.6, you have to use io.open() explicitly".  This is still true in Python 2.7, i.e. the new 3.x-compatible io library is not used by default in Python 2.  If you want to use it with subprocess.Popen, one way is to supply a pipe with an io wrapper, perhaps something like:

import io, os
i, o = os.pipe()
piperead = io.open(i,'rb',buffering=0)
p = subprocess.Popen(..., stdout=o)

See Chapter 15 of the Python 2.7 Standard Library doc for more information on the io module: http://docs.python.org/library/io.html
History
Date User Action Args
2022-04-11 14:57:33adminsetgithub: 59737
2012-08-02 05:04:28ned.deilysetstatus: open -> closed

nosy: + ned.deily
messages: + msg167189

resolution: works for me
stage: resolved
2012-08-01 22:27:34Andy.Lutomirskisettitle: "for line in file" is *still* broken in Python 2.7 -> "for line in file" is *still* broken in Python 2.7 on pipes
2012-08-01 21:56:37Andy.Lutomirskicreate