如果我使用例如“ls -Rlah/”运行以下函数“运行”,我会按预期通过打印语句立即获得输出
import subprocess32 as subprocess
def run(command):
process = subprocess.Popen(command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
try:
while process.poll() == None:
print process.stdout.readline()
finally:
# Handle the scenario if the parent
# process has terminated before this subprocess
if process.poll():
process.kill()
但是,如果我使用下面的 python 示例程序,它似乎会卡在 process.poll() 或 process.stdout.readline() 上,直到程序完成。我认为它是 stdout.readline() 因为如果我将要输出的字符串数量从 10 增加到 10000(在示例程序中)或在每次打印后添加 sys.stdout.flush() ,运行中的打印函数确实得到执行。
如何使子进程的输出更加实时?
注意:我刚刚发现 python 示例程序在输出时不执行 sys.stdout.flush(),子进程的调用者是否有办法以某种方式强制执行此操作?
每 5 秒输出 10 个字符串的示例程序。
#!/bin/env python
import time
if __name__ == "__main__":
i = 0
start = time.time()
while True:
if time.time() - start >= 5:
for _ in range(10):
print "hello world" + str(i)
start = time.time()
i += 1
if i >= 3:
break
最佳答案
在大多数系统上,命令行程序行缓冲区或 block 缓冲区取决于 stdout
是终端还是管道。在 unixy 系统上,父进程可以创建一个伪终端以获得类似终端的行为,即使子进程并不是真正从终端运行。您可以使用 pty
模块创建伪终端或使用 pexpect
模块来简化对交互式程序的访问。
如评论中所述,使用poll
读取行可能会导致数据丢失。一个例子是进程终止时留在标准输出管道中的数据。读取 pty
与管道有点不同,您会发现您需要在子进程关闭时捕获 IOError 才能使其正常工作,如下例所示。
try:
import subprocess32 as subprocess
except ImportError:
import subprocess
import pty
import sys
import os
import time
import errno
print("running %s" % sys.argv[1])
m,s = (os.fdopen(pipe) for pipe in pty.openpty())
process = subprocess.Popen([sys.argv[1]],
stdin=s,
stdout=s,
stderr=subprocess.STDOUT)
s.close()
try:
graceful = False
while True:
line = m.readline()
print line.rstrip()
except IOError, e:
if e.errno != errno.EIO:
raise
graceful = True
finally:
# Handle the scenario if the parent
# process has terminated before this subprocess
m.close()
if not graceful:
process.kill()
process.wait()
关于Python:subprocess32 process.stdout.readline()等待时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34474554/