Unbuffered read from process using subprocess in Python(在 Python 中使用子进程从进程中无缓冲读取)
问题描述
我正在尝试从产生长时间且耗时的输出的进程中读取.但是,我想捕捉它的输出以及何时.但是使用类似下面的东西似乎在缓冲命令的输出,所以我最终一次得到了输出行:
I am trying to read from a process that produces long and time-consuming output. However, I want to catch it's output as and when it is produced. But using something like the following seems to be buffering the command's output, so I end up getting the output lines all at once:
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, bufsize=0)
for line in p.stdout:
print line
我正在 MacOS 10.5 上尝试这个
I am trying this on MacOS 10.5
推荐答案
文件迭代器正在做一些内部缓冲 独立.试试这个:
The file iterator is doing some internal buffering on its own. Try this:
line = p.stdout.readline()
while line:
print line
line = p.stdout.readline()
您还需要确保您正在运行的进程实际上经常刷新其输出缓冲区.
You also need to make sure the process you are running is actually flushing its output buffers frequently.
这篇关于在 Python 中使用子进程从进程中无缓冲读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Python 中使用子进程从进程中无缓冲读取


- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01