gpt4 book ai didi

python-2.7 - Python 子进程 wait() 在 mavericks 和 Yosemite 上表现不同

转载 作者:行者123 更新时间:2023-12-02 01:43:47 25 4
gpt4 key购买 nike

我最近升级到了优胜美地。还有一些以前在 Mavericks 上运行的 Python 脚本挂了。我的版本是2.7.8。我创建了一个测试用例:

import subprocess
cat = subprocess.Popen(['top', '-l', '1'],
stdout=subprocess.PIPE,
)
cat.wait()

在 Maverics 上运行但在 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 documentation :

Popen.wait()

Wait for child process to terminate. Set and return returncode attribute.

Warning: This will deadlock when using stdout=PIPE and/or stderr=PIPE and the child process generates enough output to a pipe such that it blocks waiting for the OS pipe buffer to accept more data. Use communicate() to avoid that.

关于python-2.7 - Python 子进程 wait() 在 mavericks 和 Yosemite 上表现不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26854021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com