Popen.communicate()引发OSError:"[Errno 10] No child process". [英] Popen.communicate() throws OSError: "[Errno 10] No child processes"

查看:344
本文介绍了Popen.communicate()引发OSError:"[Errno 10] No child process".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试启动一个子进程,并使用subprocess模块​​从Python在Linux上获取其输出:

I'm trying to start up a child process and get its output on Linux from Python using the subprocess module:

#!/usr/bin/python2.4
import subprocess

p = subprocess.Popen(['ls', '-l', '/etc'],
                   stdout=subprocess.PIPE,
                   stderr=subprocess.PIPE)
out, err = p.communicate()

但是,我感到有些aki弱:有时,p.communicate()会抛出

However, I experience some flakiness: sometimes, p.communicate() would throw

OSError: [Errno 10] No child processes

什么会导致此异常?这里是否有任何不确定性或种族条件会导致脆弱?

What can cause this exception? Is there any non-determinism or race condition here that can cause flakiness?

推荐答案

您是否在脚本中拦截了SIGCHLD?如果您是Popen,则Popen将无法按预期运行,因为它依赖于它自己的信号处理程序.

Are you intercepting SIGCHLD in the script? If you are then Popen will not run as expected since it relies on it's own handler for that signal.

您可以通过注释掉Popen调用然后运行来检查SIGCHLD处理程序:

You can check for SIGCHLD handlers by commenting out the Popen call and then running:

strace python <your_script.py> | grep SIGCHLD

如果您看到类似的内容:

if you see something similar to:

rt_sigaction(SIGCHLD, ...)

然后,您遇到了麻烦.您需要在调用Popen之前禁用处理程序,然后在通信完成后将其重置(这可能会引起竞争状况,因此请当心).

then, you are in trouble. You need to disable the handler prior to calling Popen and then resetting it after communicate is done (this might introduce a race conditions so beware).

signal.signal(SIGCHLD, handler)
...
signal.signal(SIGCHLD, signal.SIG_DFL)
'''
now you can go wild with Popen. 
WARNING!!! during this time no signals will be delivered to handler
'''
...
signal.signal(SIGCHLD, handler)

据报道有一个python bug,据我所知尚未解决:

There is a python bug reported on this and as far as I see it hasn't been resolved yet:

http://bugs.python.org/issue9127

希望有帮助.

这篇关于Popen.communicate()引发OSError:"[Errno 10] No child process".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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