我在从子进程stdout管道获取输出时遇到了一些困难.我正在通过它启动一些第三方代码,以便提取日志输出.直到最近更新第三方代码,一切正常.更新后,python已无限期地开始阻塞,并且实际上没有显示任何输出.我可以手动启动第三方应用程序并查看输出.
我正在使用的代码的基本版本:
import subprocess, time
from threading import Thread
def enqueue_output(out):
print "Hello from enqueue_output"
for line in iter(out.readline,''):
line = line.rstrip("\r\n")
print "Got %s" % line
out.close()
proc = subprocess.Popen("third_party.exe", stdout=subprocess.PIPE, bufsize=1)
thread = Thread(target=enqueue_output, args=(proc.stdout,))
thread.daemon = True
thread.start()
time.sleep(30)
Run Code Online (Sandbox Code Playgroud)
如果我将third_party.exe替换为此脚本,则此方法非常有效:
import time, sys
while True:
print "Test"
sys.stdout.flush()
time.sleep(1)
Run Code Online (Sandbox Code Playgroud)
所以我不清楚魔法需要做什么才能使用原始命令.
这些都是subprocess.Popen行的所有变种我试过没有成功:
proc = subprocess.Popen("third_party.exe", stdout=subprocess.PIPE, bufsize=0)
proc = subprocess.Popen("third_party.exe", stdout=subprocess.PIPE, shell=True)
proc = subprocess.Popen("third_party.exe", stdout=subprocess.PIPE, creationflags=subprocess.CREATE_NEW_CONSOLE)
si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW
proc = subprocess.Popen("third_party.exe", …Run Code Online (Sandbox Code Playgroud)