子进程标准输入缓冲区未在 bufsize=1 的换行符上刷新

问题描述

我有两个小的 python 文件,第一个使用 input 读取一行,然后打印另一行

I have two small python files, the first reads a line using input and then prints another line

a = input()
print('complete')

第二次尝试将其作为子进程运行

The second attempts to run 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
')
print('reading')
proc.stdout.readline()

上面的脚本会先打印writing"然后是reading",然后挂起.起初我认为这是一个标准输出缓冲问题,所以我将 bufsize=1 更改为 bufsize=0,这确实解决了问题.但是,似乎是标准输入导致了问题.

The above script will print "writing" then "reading" but then hang. At first I thought this was a stdout buffering issue, so I changed bufsize=1 to bufsize=0, and this does fix the problem. However, it seems it's the stdin that's causing the problem.

使用 bufsize=1,如果我在写入下方添加 proc.stdin.flush(),该过程将继续.这两种方法看起来都很笨拙,因为(1)无缓冲的流很慢(2)到处添加刷新容易出错.为什么上面的 write 不在换行符上刷新?文档说在为子进程创建标准输入、标准输出和标准错误流时使用了 bufsize,那么是什么导致写入不刷新在换行符上?

With bufsize=1, if I add proc.stdin.flush() below the write, the process continues. Both of these approaches seem clumsy since (1) unbuffered streams are slow (2) adding flushes everywhere is error-prone. Why does the above write not flush on a newline? The docs say that bufsize is used when creating stdin, stdout, and stderr stream for the subprocess, so what's causing the write to not flush on the newline?


解决方案

来自 文档: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
')
print('reading')
proc.stdout.readline()

相关文章