我正在使用Popen
运行一个进程。我需要等待进程结束。我正在检查进程是否已通过returncode
终止。当returncode
与None
不同时,进程必须已终止。问题是,当print_output
是False
时,returncode
始终是None
,即使进程已完成运行(终止)。然而,当print_output
是True
时,情况并非如此。我正在使用以下代码运行进程:
def run(command, print_output=True):
# code mostly from: http://sharats.me/the-ever-useful-and-neat-subprocess-module.html
from subprocess import Popen, PIPE
from threading import Thread
from queue import Queue, Empty
from time import sleep
io_q = Queue()
def stream_watcher(identifier, stream):
for line in stream:
io_q.put((identifier, line))
if not stream.closed:
stream.close()
with Popen(command, stdout=PIPE, stderr=PIPE, universal_newlines=True) as proc:
if print_output:
Thread(target=stream_watcher, name='stdout-watcher', args=('STDOUT', proc.stdout)).start()
Thread(target=stream_watcher, name='stderr-watcher', args=('STDERR', proc.stderr)).start()
def printer():
while True:
try:
# Block for 1 second.
item = io_q.get(True, 1)
except Empty:
# No output in either streams for a second. Are we done?
if proc.poll() is not None:
break
else:
identifier, line = item
print(identifier + ':', line, end='')
Thread(target=printer, name='printer').start()
while proc.returncode is None:
sleep(2)
proc.poll()
if not proc.returncode == 0:
raise RuntimeError(
'The process call "{}" returned with code {}. The return code is not 0, thus an error '
'occurred.'.format(list(command), proc.returncode))
return proc.stdout, proc.stderr
有什么可能导致这个问题的线索吗?
编辑:发现了一些很奇怪的东西。我运行以下代码:
run(my_command, True)
print('--------done--------')
run(my_command, False)
print('--------done--------')
即使执行了run(my_command, False)
,也不会打印'--------done--------'
。
我不知道为什么它不起作用,但我认为这与不关闭溪流有关。以下代码有效:
TL;博士
在
subprocess.Popen()
之后添加popen.wait()
解释部分(种类)
Python运行得太快,子进程结束,但无法读取returncode
(我真的不知道它为什么这么做。欢迎解释)
我为什么用这个:
执行Shell命令并获取返回代码和输出(stdout)
另外:请阅读popen page上的
.wait
警告相关问题 更多 >
编程相关推荐