如何从 subprocess.Popen() 获取输出。 proc.stdout.readline() 阻塞,没有数据打印出来
我想要执行 Test_Pipe.py 的输出,我尝试在 Linux 上执行以下代码,但它不起作用。
Test_Pipe.py
import time
while True :
print "Someting ..."
time.sleep(.1)
Caller.py
import subprocess as subp
import time
proc = subp.Popen(["python", "Test_Pipe.py"], stdout=subp.PIPE, stdin=subp.PIPE)
while True :
data = proc.stdout.readline() #block / wait
print data
time.sleep(.1)
proc.stdout.readline() 行被阻塞,因此没有数据打印出来。
I want output from execute Test_Pipe.py, I tried following code on Linux but it did not work.
Test_Pipe.py
import time
while True :
print "Someting ..."
time.sleep(.1)
Caller.py
import subprocess as subp
import time
proc = subp.Popen(["python", "Test_Pipe.py"], stdout=subp.PIPE, stdin=subp.PIPE)
while True :
data = proc.stdout.readline() #block / wait
print data
time.sleep(.1)
The line proc.stdout.readline()
was blocked, so no data prints out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您显然可以使用 subprocess.communicate 但我认为您正在寻找实时输入和输出。
readline 被阻止,因为该进程可能正在等待您的输入。您可以逐个字符地阅读来克服这个问题,如下所示:
You obviously can use subprocess.communicate but I think you are looking for real time input and output.
readline was blocked because the process is probably waiting on your input. You can read character by character to overcome this like the following:
Nadia 的代码片段确实有效,但强烈不建议使用 1 字节缓冲区调用 read。更好的方法是使用 fcntl 将 stdout 文件描述符设置为非阻塞
,然后使用 select 测试数据是否准备好。
她是正确的,您的问题必须与您发布的 Caller.py 和 Test_Pipe 不同。 py 按规定工作。
Nadia's snippet does work but calling read with a 1 byte buffer is highly unrecommended. The better way to do this would be to set the stdout file descriptor to nonblocking using fcntl
and then using select to test if the data is ready
She was correct in that your problem must be different from what you posted as Caller.py and Test_Pipe.py do work as provided.
Test_Pipe.py
默认情况下缓冲其标准输出,因此Caller.py
中的proc
在子进程的缓冲区已满之前看不到任何输出(如果缓冲区大小为 8KB,则需要大约一分钟来填充 Test_Pipe.py 的 stdout 缓冲区)。要使输出无缓冲(文本流的行缓冲),您可以传递 < code>-u flag 到子 Python 脚本。它允许“实时”逐行读取子进程的输出:
请参阅 Python:从 subprocess.communicate 读取流输入( ),了解如何解决非 Python 子进程的块缓冲问题。
Test_Pipe.py
buffers its stdout by default soproc
inCaller.py
doesn't see any output until the child's buffer is full (if the buffer size is 8KB then it takes around a minute to fillTest_Pipe.py
's stdout buffer).To make the output unbuffered (line-buffered for text streams) you could pass
-u
flag to the child Python script. It allows to read subprocess' output line by line in "real-time":See links in Python: read streaming input from subprocess.communicate() on how to solve the block-buffering issue for non-Python child processes.
为了避免缓冲任务(例如“实时获取子进程的输出到主进程”)时可能出现的许多问题,我始终建议使用 pexpect 适用于所有非 Windows 平台,wexpect,而不是
subprocess
。To avoid the many problems that can always arise with buffering for tasks such as "getting the subprocess's output to the main process in real time", I always recommend using pexpect for all non-Windows platform, wexpect on Windows, instead of
subprocess
, when such tasks are desired.