printing to a redirected stdout from a process that was called with "2>&1 > /dev/null &"
chris at kateandchris.net
Tue Jan 30 16:39:19 CET 2007
On Mon, Jan 29, 2007 at 03:12:37PM -0800, Pappy wrote:
> SHORT VERSION:
> Python File B changes sys.stdout to a file so all 'prints' are written
> to the file. Python file A launches python file B with os.popen("./B
> 2>&^1 >dev/null &"). Python B's output disappears into never-never
> LONG VERSION:
> I am working on a site that can kick off large-scale simulations. It
> will write the output to an html file and a link will be emailed to
> the user. Also, the site will continue to display "Loading..." if the
> user wants to stick around.
> The simulation is legacy, and it basically writes its output to stdout
> (via simple print statements). In order to avoid changing all these
> prints, I simply change sys.stdout before calling the output
> functions. That works fine. The whole thing writes to an html file
> all spiffy-like.
> On the cgi end, all I want my (python) cgi script to do is check for
> form errors, make sure the server isn't crushed, run the simulation
> and redirect to a loading page (in detail, I write a constantly
> updating page to the location of the final output file. When the
> simulation is done, the constantly updating file will be magically
> replaced). The root problem is that the popen mechanism described
> above is the only way I've found to truly 'Detach' my simulation
> process. With anything else, Apache (in a *nix environment) sits and
> spins until my simulation is done. Bah.
> Any ideas?
The subprocess module is probably a good starting point:
It will allow you greater control over what happens with the output of a
process that you start. Specifically look at the the stdout and stderr
arguments to Popen. You can provide these with an open file descriptor or a
file object and it will dump the output into there.
More information about the Python-list