Python: subprocess32 process.stdout.readline() 等待时间
Python: subprocess32 process.stdout.readline() waiting time
如果我 运行 以下函数 "run" 例如 "ls -Rlah /" 我会按预期通过 print 语句立即得到输出
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
您应该刷新脚本中的标准输出:
print "hello world" + str(i)
sys.stdout.flush()
当标准输出是终端时,stdout 是行缓冲的。但如果不是,stdout 是块缓冲的,你需要明确地刷新它。
如果你不能改变脚本的来源,你可以使用 Python 的 -u
选项(在子进程中):
-u Force stdin, stdout and stderr to be totally unbuffered.
你的命令应该是:['python', '-u', 'script.py']
一般来说,这种缓冲发生在用户空间。没有强制应用程序刷新其缓冲区的通用方法:一些应用程序支持命令行选项(如 Python),其他支持信号,其他不支持任何东西。
一个解决方案可能是模拟一个伪终端,让 "hints" 程序可以在行缓冲模式下运行。尽管如此,这并不是适用于所有情况的解决方案。
对于 python 之外的其他内容,您可以尝试使用 unbuffer:
unbuffer disables the output buffering that occurs when program output is redirected from non-interactive programs. For example, suppose you are watching the output from a fifo by running it through od and then more.
od -c /tmp/fifo | more
You will not see anything until a full page of output has been produced.
You can disable this automatic buffering as follows:
unbuffer od -c /tmp/fifo | more
Normally, unbuffer does not read from stdin. This simplifies use of unbuffer in some situations. To use unbuffer in a pipeline, use the -p flag. Example:
process1 | unbuffer -p process2 | process3
所以在你的情况下:
run(["unbuffer",cmd])
文档中列出了一些注意事项,但这是另一种选择。
在大多数系统上,命令行根据 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()
如果我 运行 以下函数 "run" 例如 "ls -Rlah /" 我会按预期通过 print 语句立即得到输出
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
您应该刷新脚本中的标准输出:
print "hello world" + str(i)
sys.stdout.flush()
当标准输出是终端时,stdout 是行缓冲的。但如果不是,stdout 是块缓冲的,你需要明确地刷新它。
如果你不能改变脚本的来源,你可以使用 Python 的 -u
选项(在子进程中):
-u Force stdin, stdout and stderr to be totally unbuffered.
你的命令应该是:['python', '-u', 'script.py']
一般来说,这种缓冲发生在用户空间。没有强制应用程序刷新其缓冲区的通用方法:一些应用程序支持命令行选项(如 Python),其他支持信号,其他不支持任何东西。
一个解决方案可能是模拟一个伪终端,让 "hints" 程序可以在行缓冲模式下运行。尽管如此,这并不是适用于所有情况的解决方案。
对于 python 之外的其他内容,您可以尝试使用 unbuffer:
unbuffer disables the output buffering that occurs when program output is redirected from non-interactive programs. For example, suppose you are watching the output from a fifo by running it through od and then more. od -c /tmp/fifo | more You will not see anything until a full page of output has been produced. You can disable this automatic buffering as follows:
unbuffer od -c /tmp/fifo | more
Normally, unbuffer does not read from stdin. This simplifies use of unbuffer in some situations. To use unbuffer in a pipeline, use the -p flag. Example: process1 | unbuffer -p process2 | process3
所以在你的情况下:
run(["unbuffer",cmd])
文档中列出了一些注意事项,但这是另一种选择。
在大多数系统上,命令行根据 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()