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

Python3 unbuffered stdin #63068

Closed
joedborg mannequin opened this issue Aug 28, 2013 · 3 comments
Closed

Python3 unbuffered stdin #63068

joedborg mannequin opened this issue Aug 28, 2013 · 3 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@joedborg
Copy link
Mannequin

joedborg mannequin commented Aug 28, 2013

BPO 18868
Nosy @pitrou, @bitdancer, @joedborg

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 = None
closed_at = <Date 2013-08-28.17:53:46.392>
created_at = <Date 2013-08-28.09:19:24.881>
labels = ['type-bug', 'invalid']
title = 'Python3 unbuffered stdin'
updated_at = <Date 2013-08-28.17:53:46.390>
user = 'https://github.com/joedborg'

bugs.python.org fields:

activity = <Date 2013-08-28.17:53:46.390>
actor = 'pitrou'
assignee = 'none'
closed = True
closed_date = <Date 2013-08-28.17:53:46.392>
closer = 'pitrou'
components = []
creation = <Date 2013-08-28.09:19:24.881>
creator = 'Joe.Borg'
dependencies = []
files = []
hgrepos = []
issue_num = 18868
keywords = []
message_count = 3.0
messages = ['196359', '196385', '196395']
nosy_count = 4.0
nosy_names = ['pitrou', 'r.david.murray', 'sbt', 'Joe.Borg']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue18868'
versions = ['Python 3.3', 'Python 3.4']

@joedborg
Copy link
Mannequin Author

joedborg mannequin commented Aug 28, 2013

I'm in need of an unbuffered stdin for Python3. Using the '-u' flag worked fine in Python2. But, it seems, Python3's stdin is always buffered; as seen in http://bugs.python.org/issue4705.

This is not always desirable. For example:

#!/bin/python3
import os, subprocess, time

with open("%s/unbuffered_test.log" % (os.getenv("HOME")), "w") as f:
    with subprocess.Popen(["%s/unbuffered_test.sh" % (os.getenv("HOME"))], stdin=subprocess.PIPE, stdout=f, stderr=f) as p:
        p.stdin.write(bytes("test\n", encoding="utf-8"))
        time.sleep(10)

Where unbuffered_test.sh is:
#!/bin/sh
read INPUT
echo $INPUT
exit 0

Running with -u in Python2 sees the log file populated before the 10 seconds are up. This isn't the case in Python3. This making controlling applications, using subprocess, basically impossible; without putting p.stdin.flush() after each command (which does work in the example above).

I ran this example in Python3.3.2.

@joedborg joedborg mannequin added the type-bug An unexpected behavior, bug, or error label Aug 28, 2013
@sbt
Copy link
Mannequin

sbt mannequin commented Aug 28, 2013

Try using Popen(..., bufsize=0).

@pitrou
Copy link
Member

pitrou commented Aug 28, 2013

Indeed, this is not related to sys.stdin, but to Popen's own buffering, and the fix is either to pass "bufsize=0" or to flush() when you need to.

I'm closing as invalid, don't hesitate to re-open if I misunderstood something.

@pitrou pitrou closed this as completed Aug 28, 2013
@pitrou pitrou added the invalid label Aug 28, 2013
@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
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant