我正在尝试使用 Python 获取另一个脚本的输出subprocess.Popen
就像下面这样
process = Popen(command, stdout=PIPE, shell=True)
exitcode = process.wait()
output = process.stdout.read() # hangs here
仅当我将其作为 python 脚本运行并且无法在 python shell 中重现时,它才会挂在第三行。
另一个脚本只打印几个单词,我假设这不是缓冲区问题。
有谁知道我在这里做错了什么?
你可能想使用.communicate() http://docs.python.org/2/library/subprocess.html#subprocess.Popen.communicate而不是.wait()
plus .read()
。请注意有关警告wait()
on the subprocess
文档页面:
警告这在使用时会死锁stdout=PIPE
and/or stderr=PIPE
并且子进程生成足够的输出到管道,以便它阻塞等待操作系统管道缓冲区接受更多数据。使用communicate()
以避免这种情况。
http://docs.python.org/2/library/subprocess.html#subprocess.Popen.wait http://docs.python.org/2/library/subprocess.html#subprocess.Popen.wait
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)