Can I pass a file to Popen and still have it run asynchronously
<p>I have a Django based server and I'm calling a script which does a bunch of work. This needs to be asynchronous so I'm using Popen. However for debugging I want to redirect stdout and stderr from PIPE to a file. Will this affect the asynchronous performance?</p>

<p>How should I make sure the file opens and closes properly when the python script itself is already done (and has made a return call.)</p>

<p>It depends on how asynchronous your program needs to be. Writes to stdout go to the operating system's file cache which is normally fast but may wait from time to time as data is flushed to the disk. Normally that's not a problem.</p>

<p>Since stdout is not a console, it will do buffered writes and that can be a problem if the program hits a non-standard error that skips flushing the pipe. The buffering can also delay what is in the file so for instance an external program cat'ing the file may not see logs right away.</p>

<p>The parent process should close its view of the file after starting the child and the operating system will take care of closing the file when the process terminates.</p>

<pre><code>with open('stdout', 'w') as out, open('stderr', 'w') as err:
    proc = subprocess.Popen(['myprog'], stdout=out, stderr=err)
</code></pre>

python asynchronous subprocess