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 stdin.flush does not flush #58208

Closed
amoffat mannequin opened this issue Feb 13, 2012 · 3 comments
Closed

Subprocess stdin.flush does not flush #58208

amoffat mannequin opened this issue Feb 13, 2012 · 3 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@amoffat
Copy link
Mannequin

amoffat mannequin commented Feb 13, 2012

BPO 14000

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 2012-02-13.16:40:06.528>
created_at = <Date 2012-02-13.09:54:09.160>
labels = ['type-bug', 'invalid']
title = 'Subprocess stdin.flush does not flush'
updated_at = <Date 2013-06-18.07:39:48.394>
user = 'https://bugs.python.org/amoffat'

bugs.python.org fields:

activity = <Date 2013-06-18.07:39:48.394>
actor = 'wenjianhn'
assignee = 'none'
closed = True
closed_date = <Date 2012-02-13.16:40:06.528>
closer = 'rosslagerwall'
components = ['None']
creation = <Date 2012-02-13.09:54:09.160>
creator = 'amoffat'
dependencies = []
files = []
hgrepos = []
issue_num = 14000
keywords = []
message_count = 3.0
messages = ['153257', '153280', '191388']
nosy_count = 3.0
nosy_names = ['rosslagerwall', 'amoffat', 'wenjianhn']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue14000'
versions = ['Python 3.1', 'Python 2.7', 'Python 3.2']

@amoffat
Copy link
Mannequin Author

amoffat mannequin commented Feb 13, 2012

The following code only works if the "p.stdin.close()" line is uncommented. It seems that stdin only flushes to the process on stdin.close(), not on stdin.flush().

-----------------------------------

import subprocess as subp
import threading
import time


p = subp.Popen(["tr", "[:lower:]", "[:upper:]"],
    stdin=subp.PIPE, stderr=subp.PIPE, stdout=subp.PIPE, close_fds=True)


def read(stdout):
    while True:
        line = stdout.readline()
        if not line: break
        print(line.decode("utf8"))

t = threading.Thread(target=read, args=(p.stdout,))
t.daemon = True
t.start()

p.stdin.write("uppercase\n".encode())
p.stdin.flush()
#p.stdin.close()

time.sleep(1)

@amoffat amoffat mannequin added the type-bug An unexpected behavior, bug, or error label Feb 13, 2012
@rosslagerwall
Copy link
Mannequin

rosslagerwall mannequin commented Feb 13, 2012

This appears to be a buffering issue with the tr program. Replace with ["cat", "-"] and it works whether the close() is in or not.

To fix this, you need to open up the child process so that it is connected to a tty. man 4 pts if you want to investigate this further.

@rosslagerwall rosslagerwall mannequin closed this as completed Feb 13, 2012
@rosslagerwall rosslagerwall mannequin added the invalid label Feb 13, 2012
@wenjianhn
Copy link
Mannequin

wenjianhn mannequin commented Jun 18, 2013

The following code shows how to use pts.

#!/usr/bin/env python

import os
import pty
import shlex
import time

_args = "/usr/bin/ssh example.com"
args = shlex.split(_args)

pid, child_fd = pty.fork()

if pid == 0:
    # Child
    os.execv("/usr/bin/ssh", args)

else:
# Parent
while True:
os.write(child_fd, '# keep alive\n')
os.read(child_fd, 1024)

    time.sleep(2)

@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

0 participants