Subprocess stdin buffer not clearing on a new line with bufsize = 1

I have two small python files, the first reads a line with inputand then prints another line

a = input()
print('complete')

Second attempt to execute this as a subprocess

import subprocess

proc = subprocess.Popen('./simp.py',
                        stdout=subprocess.PIPE,
                        stdin=subprocess.PIPE,
                        bufsize=1)
print('writing')
proc.stdin.write(b'hey\n')
print('reading')
proc.stdout.readline()

The above script will print “write”, then “read”, but then freeze. At first I thought it was a stdout buffering problem, so I changed bufsize=1to bufsize=0, and this fixes the problem. However, it seems that stdin is causing the problem.

bufsize=1, proc.stdin.flush() , . , (1) (2) . write ? , bufsize stdin, stdout stderr , ?

+4
1

docs: "1 , ( , universal_newlines = True, )". :

import subprocess

proc = subprocess.Popen('./simp.py',
                        stdout=subprocess.PIPE,
                        stdin=subprocess.PIPE,
                        bufsize=1,
                        universal_newlines=True)

print('writing')
proc.stdin.write('hey\n')
print('reading')
proc.stdout.readline()
+4

Source: https://habr.com/ru/post/1568366/


All Articles