使用 subprocess.PIPE 进行增量输出
我正在使用 subprocess
模块从另一个应用程序运行命令我知道您可以执行以下操作
import subprocess
app = subprocess(args, stdout=subprocess.PIPE)
out, err = app.communicate()
print out
我希望输出显示为最后发生的一个大斑点。有想法吗?
I am using subprocess
module for running commands from another app I know that you are able to do the following
import subprocess
app = subprocess(args, stdout=subprocess.PIPE)
out, err = app.communicate()
print out
I would like the output to be displayed as it happening as supposed to one big blob at the end. Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题可能是您在子进程中运行的命令缓冲了其输出:在这种情况下,在 Blender 的回答中,
process.stdout.read(1)
直到子进程的输出缓冲区才会返回。子进程已满,导致其被刷新,从而被父进程看到。请参阅此答案和这个 了解有关缓冲如何影响事物的更多信息。
下面是一些脚本来说明:
现在
,将两个脚本放在同一目录中并运行 python runspew.py。您会看到类似这样的内容:
现在,从
spew.py
中的sys.stdout.flush()
中删除注释并执行python runspew.py 再次。现在,您将看到类似这样的内容:
请注意,
runspew.py
没有更改:仅更改了作为子进程运行的程序。如果您正在运行的程序无法以无缓冲方式运行(或频繁刷新),那么您必须使用expect
/unbuffer
,如另一个中所述我链接到的答案。The problem may be that the command you are running in the subprocess buffers its output: in which case, in Blender's answer, the
process.stdout.read(1)
would not return until the output buffer of the subprocess filled up, causing it to be flushed, and thus seen by the parent process.See this answer and this one for more information about how buffering affects things.
Here's some scripts to illustrate:
and
Now, put both scripts in the same directory and run
python runspew.py
. You'll see something like this:Now, remove the comment from
sys.stdout.flush()
inspew.py
and dopython runspew.py
again. You'll now see something like this:Notice that
runspew.py
didn't change: only the program which was run as the subprocess did. If the program you're running can't be made to run unbuffered (or flush frequently), then you'll have to useexpect
/unbuffer
as described in the other answers I linked to.我曾经做过一个 Python shell 实现,所以这是我用来运行命令的代码。它运行
wget
和nano
很好,所以我认为代码会满足您的需求:基本上,您必须直接写入
stdout
。I made a Python shell implementation once, so here's the code I used to run commands. It ran
wget
andnano
fine, so I think the code would suit your needs:Basically, you have to write directly into
stdout
.