使用 subprocess.PIPE 进行增量输出

发布于 2024-11-14 16:48:10 字数 227 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幽梦紫曦~ 2024-11-21 16:48:10

问题可能是您在子进程中运行的命令缓冲了其输出:在这种情况下,在 Blender 的回答中,process.stdout.read(1) 直到子进程的输出缓冲区才会返回。子进程已满,导致其被刷新,从而被父进程看到。

请参阅此答案这个 了解有关缓冲如何影响事物的更多信息。

下面是一些脚本来说明:

# spew.py
import sys
import time

for i in range(10):
    print 'Message no. %d' % i
    #sys.stdout.flush()
    time.sleep(1)

现在

# runspew.py
import subprocess
import time

start = time.time()
p = subprocess.Popen(['python', 'spew.py'], stdout=subprocess.PIPE)
data = p.stdout.read(1)
print 'Elapsed time to first byte read: %.2f' % (time.time() - start)
data = p.stdout.read()
print 'Elapsed time to last byte read: %.2f' % (time.time() - start)
p.wait()

,将两个脚本放在同一目录中并运行 python runspew.py。您会看到类似这样的内容:

Elapsed time to first byte read: 10.05
Elapsed time to last byte read: 10.05

现在,从 spew.py 中的 sys.stdout.flush() 中删除注释并执行 python runspew.py 再次。现在,您将看到类似这样的内容:

Elapsed time to first byte read: 0.02
Elapsed time to last byte read: 10.04

请注意,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:

# spew.py
import sys
import time

for i in range(10):
    print 'Message no. %d' % i
    #sys.stdout.flush()
    time.sleep(1)

and

# runspew.py
import subprocess
import time

start = time.time()
p = subprocess.Popen(['python', 'spew.py'], stdout=subprocess.PIPE)
data = p.stdout.read(1)
print 'Elapsed time to first byte read: %.2f' % (time.time() - start)
data = p.stdout.read()
print 'Elapsed time to last byte read: %.2f' % (time.time() - start)
p.wait()

Now, put both scripts in the same directory and run python runspew.py. You'll see something like this:

Elapsed time to first byte read: 10.05
Elapsed time to last byte read: 10.05

Now, remove the comment from sys.stdout.flush() in spew.py and do python runspew.py again. You'll now see something like this:

Elapsed time to first byte read: 0.02
Elapsed time to last byte read: 10.04

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 use expect/unbuffer as described in the other answers I linked to.

煞人兵器 2024-11-21 16:48:10

我曾经做过一个 Python shell 实现,所以这是我用来运行命令的代码。它运行wgetnano很好,所以我认为代码会满足您的需求:

  process = subprocess.Popen(shlex.split(command), stdout = subprocess.PIPE, stderr = subprocess.STDOUT)

  while True:
    output = process.stdout.read(1)

    if output == '' and process.poll() != None:
      break

    if output != '':
      sys.stdout.write(output)
      sys.stdout.flush()

基本上,您必须直接写入stdout

I made a Python shell implementation once, so here's the code I used to run commands. It ran wget and nano fine, so I think the code would suit your needs:

  process = subprocess.Popen(shlex.split(command), stdout = subprocess.PIPE, stderr = subprocess.STDOUT)

  while True:
    output = process.stdout.read(1)

    if output == '' and process.poll() != None:
      break

    if output != '':
      sys.stdout.write(output)
      sys.stdout.flush()

Basically, you have to write directly into stdout.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文