Python 子进程 wait() 在 Mavericks 和 Yosemite 上的行为不同 [英] Python subprocess wait() behaves differently on mavericks and Yosemite

查看:78
本文介绍了Python 子进程 wait() 在 Mavericks 和 Yosemite 上的行为不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I recently upgraded to Yosemite. And some of the Python scripts are hanging that used to run on Mavericks. My version is 2.7.8. I created a test case:

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

在 Maveric 上运行,但在 Yosemite 上挂起.当我在优胜美地打断时,我看到以下回溯.

Runs on Maverics but hangs on Yosemite. When I interrupt on Yosemite, I see the following traceback.

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

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

Any hint as to what am I doing wrong?

推荐答案

看来您需要调用 communicate().来自 Python 文档:

It looks like you need to call communicate(). From the Python documentation:

Popen.wait()

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

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

警告:这会在使用 stdout=PIPE 和/或 stderr=PIPE 和子进程生成足够的输出到管道时死锁以至于它等待 OS 管道缓冲区接受更多数据的块.用communicate() 到避免这种情况.

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 子进程 wait() 在 Mavericks 和 Yosemite 上的行为不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆