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

subprocess stdin buffer not flushing on newline with bufsize=1(子进程标准输入缓冲区未在 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 的换行符上刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)