您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用Python子进程在stdout上捕获C程序的输出?期待?

使用Python子进程在stdout上捕获C程序的输出?期待?

您不能像这样使用p.stdout;如果您要求“整个标准输出”,则仅在过程终止(或填充管道缓冲区,这可能需要很长时间)时才可用。

您需要逐行从流程的标准输出中读取。

while True:
    ln = p.stdout.readline()
    if '' == ln:
        break
    m = re.search("Thread (?P<id>\d+)", ln);
    if m:
        # use m.group() to extract information
        # e.g. m.group('id') will hold the 12345 from "Thread 12345"

最好将stdout设置为行缓冲(通常在可能的情况下完全缓冲),但是我认为这只能在被调用的程序中完成。

这里有两个要考虑的缓冲区。一种是C程序的输出缓冲区。这可能不存在(无缓冲输出),行缓冲或完全缓冲(1K,4K或8K是一些可能的大小)。

在程序中,将调用“ printf()”。输出为:

现在,输出进入Python的管道。再次可以完全缓冲(stdout)或行缓冲(readline)。因此输出为:

在后一种情况下,缓冲区将以4K块的形式进入Python逻辑。

现在让我们想象一下一个 C程序,它每秒将一条长1K字符的行输出到Python程序中(如果C程序已被完全缓冲,那么将无能为力!)

循环读取stdout,我们将看到(在for循环内):

通过readline阅读将得到:

在这里,我运行“ ping -c 3 -i 2 127.0.0.1”,以便以两秒的间隔将三个数据包发送到本地主机。一次ping需要大约六秒钟。我从ping读取了输出,并打印了一个时间戳。ping的整个输出足够小,可以放入Python的完整缓冲区中。

#!/usr/bin/python

import subprocess
from time import gmtime, strftime

p = subprocess.Popen(["ping", "-c", "3", "-i", "2", "127.0.0.1"],
                 stdout=subprocess.PIPE)

for ln in p.stdout:
    print strftime("%H:%M:%s", gmtime()) + " received " + ln

# Now I start the same process again, reading the input the other way.

p = subprocess.Popen(["ping", "-c", "3", "-i", "2", "127.0.0.1"],
                 stdout=subprocess.PIPE)

while True:
    ln = p.stdout.readline()
    if '' == ln:
            break
    print strftime("%H:%M:%s", gmtime()) + " received " + ln

我在Linux机器上收到的输出是预期的:

(nothing for the first six seconds)
15:40:10 received PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
15:40:10 received 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.037 ms
15:40:10 received 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.034 ms
15:40:10 received 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.031 ms
15:40:10 received
15:40:10 received --- 127.0.0.1 ping statistics ---
15:40:10 received 3 packets transmitted, 3 received, 0% packet loss, time 3998ms
15:40:10 received rtt min/avg/max/mdev = 0.031/0.034/0.037/0.002 ms

15:40:10 received PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
15:40:10 received 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.041 ms
15:40:12 received 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.039 ms
15:40:14 received 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.035 ms
15:40:14 received
15:40:14 received --- 127.0.0.1 ping statistics ---
15:40:14 received 3 packets transmitted, 3 received, 0% packet loss, time 3999ms
15:40:14 received rtt min/avg/max/mdev = 0.035/0.038/0.041/0.005 ms
python 2022/1/1 18:31:02 有203人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶