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()
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?
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.
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, ...)
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)
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
Hope that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With