我最近升级到优胜美地。一些用于在 Mavericks 上运行的 Python 脚本被挂起。我的版本是2.7.8。我创建了一个测试用例:
import subprocess
cat = subprocess.Popen(['top', '-l', '1'],
stdout=subprocess.PIPE,
)
cat.wait()
在 Maverics 上运行,但在 Yosemite 上挂起。当我中断 Yosemite 时,我看到以下回溯。
Traceback (most recent call last):
File "test.py", line 5, in <module>
cat.wait()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1376, in wait
pid, sts = _eintr_retry_call(os.waitpid, self.pid, 0)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 476, in _eintr_retry_call
return func(*args)
KeyboardInterrupt
关于我做错了什么有任何提示吗?
看起来你需要打电话communicate()
。来自Python 文档 https://docs.python.org/2/library/subprocess.html#popen-objects:
Popen.wait()
等待子进程终止。设置并返回returncode
属性。
Warning:使用时会出现死锁stdout=PIPE
and/or stderr=PIPE
并且子进程生成足够的输出到管道,以便它
阻塞等待操作系统管道缓冲区接受更多数据。使用communicate() https://docs.python.org/2/library/subprocess.html#subprocess.Popen.communicate以避免这种情况。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)