Python C程序子进程挂起“for line in iter”

好的,我试图从python脚本运行一个C程序.目前我正在使用测试C程序:

#include <stdio.h>

int main() {
while (1) {
    printf("2000\n");
    sleep(1);
}
return 0;
}

模拟我将要使用的程序,它不断地从传感器获取读数.
然后我试图在C程序中使用python中的子进程读取输出(在本例中为“2000”):

#!usr/bin/python
import subprocess

process = subprocess.Popen("./main", stdout=subprocess.PIPE)
while True:
    for line in iter(process.stdout.readline, ''):
            print line,

但这不起作用.从使用print语句开始,它运行.Popen行然后等待iter中的行(process.stdout.readline,”):,直到我按下Ctrl-C.

为什么是这样?这正是我见过的大多数示例都是他们的代码,但它没有读取文件.

编辑:

有没有办法让它只在有东西被阅读时才能运行?

解决方法:

这是一个块缓冲问题.

以下是对我的Python: read streaming input from subprocess.communicate()问题答案的案例版本的扩展.

直接修复C程序中的stdout缓冲区

基于stdio的程序作为规则是行缓冲的,如果它们在终端中以交互方式运行,并且当它们的stdout被重定向到管道时阻止缓冲.在后一种情况下,在缓冲区溢出或刷新之前,您将看不到新行.

为了避免在每次printf()调用后调用fflush(),你可以通过在一开始调用C程序来强制行缓冲输出:

setvbuf(stdout, (char *) NULL, _IOLBF, 0); /* make line buffered stdout */

一旦打印换行符,就会在这种情况下刷新缓冲区.

或者修改它而不修改C程序的源代码

有stdbuf实用程序,允许您在不修改源代码的情况下更改缓冲类型,例如:

from subprocess import Popen, PIPE

process = Popen(["stdbuf", "-oL", "./main"], stdout=PIPE, bufsize=1)
for line in iter(process.stdout.readline, b''):
    print line,
process.communicate() # close process' stream, wait for it to exit

还有其他可用的实用程序,见Turn off buffering in pipe.

或者使用伪TTY

为了让子进程认为它是以交互方式运行,你可以使用pexpect module或它的类比,对于使用pexpect和pty模块的代码示例,参见Python subprocess readlines() hangs.这里提供了pty示例的变体(它应该适用于Linux):

#!/usr/bin/env python
import os
import pty
import sys
from select import select
from subprocess import Popen, STDOUT

master_fd, slave_fd = pty.openpty()  # provide tty to enable line buffering
process = Popen("./main", stdin=slave_fd, stdout=slave_fd, stderr=STDOUT,
                bufsize=0, close_fds=True)
timeout = .1 # ugly but otherwise `select` blocks on process' exit
# code is similar to _copy() from pty.py
with os.fdopen(master_fd, 'r+b', 0) as master:
    input_fds = [master, sys.stdin]
    while True:
        fds = select(input_fds, [], [], timeout)[0]
        if master in fds: # subprocess' output is ready
            data = os.read(master_fd, 512) # <-- doesn't block, may return less
            if not data: # EOF
                input_fds.remove(master)
            else:
                os.write(sys.stdout.fileno(), data) # copy to our stdout
        if sys.stdin in fds: # got user input
            data = os.read(sys.stdin.fileno(), 512)
            if not data:
                input_fds.remove(sys.stdin)
            else:
                master.write(data) # copy it to subprocess' stdin
        if not fds: # timeout in select()
            if process.poll() is not None: # subprocess ended
                # and no output is buffered <-- timeout + dead subprocess
                assert not select([master], [], [], 0)[0] # race is possible
                os.close(slave_fd) # subproces don't need it anymore
                break
rc = process.wait()
print("subprocess exited with status %d" % rc)

或者通过pexpect使用pty

pexpect将pty处理包装到higher level interface

#!/usr/bin/env python
import pexpect

child = pexpect.spawn("/.main")
for line in child:
    print line,
child.close()

Q: Why not just use a pipe (popen())?解释了为什么伪TTY有用.

上一篇:12.2 删除物化视图的是hang住了


下一篇:Censoring 系列题解