When starting a process with POpen I want to capture the stderr and stdout pipes. My code works but has a race condition between the process being executed and the stream being wrapped. This manifests itself by some stdout bleed into the console before being wrapped when the scheduler returns to the Python program.
# Start process.
proc = subprocess.Popen(cmd_args, env=context, cwd=cwd,
stdout=subprocess.PIPE if capture_out else subprocess.DEVNULL,
stderr=subprocess.PIPE if capture_err else subprocess.DEVNULL,
stdin=None)
# Wrap streams.
out_stream = NonBlockingStreamReader(proc.stdout) if capture_out else None
err_stream = NonBlockingStreamReader(proc.stderr) if capture_err else None
How can I wrap the streams before passing them into subprocess.Popen
?