I need to launch a number of long-running processes with subprocess.Popen
, and would like to have the stdout
and stderr
from each automatically piped to separate log files. Each process will run simultaneously for several minutes, and I want two log files (stdout
and stderr
) per process to be written to as the processes run.
Do I need to continually call p.communicate()
on each process in a loop in order to update each log file, or is there some way to invoke the original Popen
command so that stdout
and stderr
are automatically streamed to open file handles?
Per the docs,
So just pass the open-for-writing file objects as named arguments
stdout=
andstderr=
and you should be fine!I am simultaneously running two subprocesses, and saving the output from both into a single log file. I have also built in a timeout to handle hung subprocesses. When the output gets too big, the timeout always triggers, and none of the stdout from either subprocess gets saved to the log file. The answer posed by Alex above does not solve it.
You can pass
stdout
andstderr
as parameters toPopen()
For example