subprocess stdin buffer not flushing on newline with bufsize=1(子进程标准输入缓冲区未在 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()
这篇关于子进程标准输入缓冲区未在 bufsize=1 的换行符上刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:子进程标准输入缓冲区未在 bufsize=1 的换行符上刷新
基础教程推荐
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
