[issue2475] Popen.poll always returns None

roudkerk report at bugs.python.org
Thu Apr 3 19:39:03 CEST 2008


roudkerk <r.m.oudkerk at gmail.com> added the comment:

The problem is that os.wait() does not play nicely with subprocess.py.
Popen.poll() and Popen.wait() use os.waitpid(pid, ...)  which will
raise OSError if pid has already been reported by os.wait().
Popen.poll() swallows OSError and by default returns None.

You can (sort of) fix your program by using
"p.popen(_deadstate='dead')" in place of "p.popen()".  This will make
poll() return 'dead' instead of None if OSError gets caught, but this
is undocumented.

Maybe a subprocess.wait() function could be added which would return a
tuple

    (pid, exitcode, popen_object)

where popen_object is None if the process is "foreign" (i.e. it was
not created by the subprocess module).

It would not be hard to implement this on unix if you don't care about
thread safety.  (On unix Popen.wait() is not thread-safe either, so
maybe thread safety does not matter.)

To implement something similar on windows you would probably need to
use WaitForMultipleObjects() to check whether any process handles are
signalled, but that would involve patching _subprocess.c or using
ctypes or pywin32.

----------
nosy: +roudkerk

__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue2475>
__________________________________


More information about the Python-bugs-list mailing list