【问题标题】:Python subprocess wait() behaves differently on mavericks and YosemitePython 子进程 wait() 在 Mavericks 和 Yosemite 上的行为不同
【发布时间】:2015-01-07 08:38:35
【问题描述】:

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

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

在 Maveric 上运行,但在 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

关于我做错了什么的任何提示?

【问题讨论】:

  • 执行['top', '-l', '1', '-n', '0'] 之类的操作并检查是否可行。这可能意味着指向@GoBusto 的死锁是正确的。

标签: python-2.7 subprocess osx-yosemite


【解决方案1】:

您似乎需要致电communicate()。来自Python documentation

Popen.wait()

等待子进程终止。设置并返回returncode 属性。

警告:这将在使用stdout=PIPE 和/或stderr=PIPE 时出现死锁,并且子进程会向管道生成足够的输出,从而 等待 OS 管道缓冲区接受更多数据的块。采用 communicate() 避免这种情况。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-06
    • 2021-10-03
    • 2011-03-01
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    • 2011-02-12
    • 1970-01-01
    相关资源
    最近更新 更多