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

这是一个块缓冲问题.

直接修复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

或者使用伪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) #

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

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

#!/usr/bin/env python

import pexpect

child = pexpect.spawn("/.main")

for line in child:

print line,

child.close()

你可能感兴趣的:(python,for,line,in,lines)