【发布时间】:2013-12-10 04:17:19
【问题描述】:
我需要从我的脚本中运行一个子进程。子进程是一个交互式(类似 shell)应用程序,我通过子进程'stdin 向它发出命令。
在我发出命令后,子进程将结果输出到stdout,然后等待下一个命令(但不会终止)。
例如:
from subprocess import Popen, PIPE
p = Popen(args = [...], stdin = PIPE, stdout = PIPE, stderr = PIPE, shell = False)
# Issue a command:
p.stdin.write('command\n')
# *** HERE: get the result from p.stdout ***
# CONTINUE with the rest of the script once there is not more data in p.stdout
# NOTE that the subprocess is still running and waiting for the next command
# through stdin.
我的问题是从p.stdout 获取结果。脚本需要在p.stdout中有新数据时获取输出;但是一旦没有更多数据,我想继续执行脚本。
子进程没有终止,所以我不能使用communicate()(它等待进程终止)。
发出命令后,我尝试从p.stdout 读取,如下所示:
res = p.stdout.read()
但是子进程不够快,我得到的结果是空的。
我想过循环轮询p.stdout,直到我得到一些东西,但是我怎么知道我得到了一切?无论如何,这似乎很浪费。
有什么建议吗?
【问题讨论】:
-
尝试使用异步库/框架,例如电路(例如:bitbucket.org/circuits/circuits-dev/src/tip/examples/ping.py)
-
@JamesMills,异步可能可行,但我认为这会使我想做的事情过于复杂。如果没有其他解决方案出现,我会这样做。谢谢。
-
唯一的其他方法是使用线程来隐藏阻塞调用。任何其他选项都将涉及某种并发/异步库/框架。你也可以使用一个:)
标签: python subprocess