python popen.stdout.readline() 挂起

jon*_*opf 4 python stdout popen

我遇到了一个问题...有谁知道为什么这段代码挂在 while 循环中。循环似乎没有捕捉到stdout.

working_file = subprocess.Popen(["/pyRoot/iAmACrashyProgram"], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)

line = working_file.stdout.readline()
working_file.stdout.flush()
while working_file != "" :
    print(line)
    line = working_file.stdout.readline()
    working_file.stdout.flush()
Run Code Online (Sandbox Code Playgroud)

脚本挂起,光标在readline()遇到时会闪烁。我不明白为什么。任何人都可以透露一些信息吗?

Joh*_*Doe 5

进行非阻塞读取对您有帮助吗?

import fcntl
import os

def nonBlockReadline(output):
    fd = output.fileno()
    fl = fcntl.fcntl(fd, fcntl.F_GETFL)
    fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
    try:
        return output.readline()
    except:
        return ''

working_file = subprocess.Popen(["/pyRoot/iAmACrashyProgram"], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)

line = nonBlockReadline(working_file.stdout)
working_file.stdout.flush()
while working_file != "" :
    print(line)
    line = nonBlockReadline(working_file.stdout)
    working_file.stdout.flush()
Run Code Online (Sandbox Code Playgroud)

我不确定你到底想做什么,但这会更好吗?它只是读取所有数据,而不是一次只读取一行。它对我来说更具可读性。

import fcntl
import os

def nonBlockRead(output):
    fd = output.fileno()
    fl = fcntl.fcntl(fd, fcntl.F_GETFL)
    fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
    try:
        return output.read()
    except:
        return ''

working_file = subprocess.Popen(["/pyRoot/iAmACrashyProgram"], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)

stdout = ''

while working_file.poll() is None:
    stdout += nonBlockRead(working_file.stdout)

# we can probably save some time and just print it instead...
#print(stdout)

stdout = stdout.splitlines()
for line in stdout:
    print(line)
Run Code Online (Sandbox Code Playgroud)

编辑:应该更适合您的用例的通用脚本:

import fcntl
import os

def nonBlockRead(output):
    fd = output.fileno()
    fl = fcntl.fcntl(fd, fcntl.F_GETFL)
    fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
    try:
        return output.read()
    except:
        return ''

working_file = subprocess.Popen(["/pyRoot/iAmACrashyProgram"], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)

while working_file.poll() is None:
    stdout = nonBlockRead(working_file.stdout)

    # make sure it returned data
    if stdout:
        # process data
        working_file.stdin.write(something)
Run Code Online (Sandbox Code Playgroud)