subprocess.Popen and thread module

Nobody nobody at nowhere.com
Thu Aug 11 10:15:13 CEST 2011


Danny Wong (dannwong) <dannwong at cisco.com> wrote:

>        cmd_output = subprocess.Popen(['scm', 'load', '--force',
> '-r', nickname, '-d', directory, project], stdout=subprocess.PIPE,
> stderr=subprocess.PIPE)
>       status = cmd_output.wait()

If you redirect stdout and/or stderr to a pipe, you must wait for EOF
before calling wait(), otherwise you risk deadlock (the process blocks
waiting for Python to read data from the pipe while Python is blocked
waiting for the process to terminate).

And if you redirect both stdout and stderr to pipes, you must either use
multiple threads or non-blocking I/O, otherwise you risk deadlock (the
process blocks waiting for Python to read data from one of the pipes while
Python is blocked waiting for the process to write to the other pipe).

I suggest looking at the .communicate() method of the Popen class.




More information about the Python-list mailing list