Problems with background processes on Windows

geoff.bache at gmail.com geoff.bache at gmail.com
Fri Mar 27 18:22:22 EDT 2009


Hi all,

The following code behaves differently on Windows and Linux using
Python 2.5.2. The Linux behaviour is what I expect in both places :)
Perhaps somebody could help explain this. Or maybe it is a Python bug.
Or a Windows feature...

---- communicate.py ---

import subprocess
p = subprocess.Popen([ "python", "sleep.py" ], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
p.communicate()
-----------------------
---- sleep.py ----

import subprocess, os
subprocess.Popen([ 'python', '-c', 'import time; time.sleep(10)' ],
stdin=open(os.devnull), stdout=open(os.devnull, "w"),
stderr=subprocess.STDOUT)
--------------------

In short, we start a subprocess which in turn starts a different
subprocess and then returns without waiting for it. We then try to
"communicate" with the first subprocess.

On Windows if I run "communicate.py" it does not return for 10
seconds, i.e. until the "grandchild" background process terminates. On
Linux it returns immediately as I would expect.

If I replace "p.communicate()" with "p.wait()" it returns immediately
on both. If I don't point stdin, stdout and stderr of the child
process to os.devnull then it will wait 10 seconds on Linux also,
which I'd also expect as we can't collect info from these places if
they've been forwarded elsewhere. It seems like Windows somehow
doesn't notice this.

Any help gratefully appreciated.

Regards,
Geoff Bache



More information about the Python-list mailing list