views:

269

answers:

2

I'm trying to launch a .py script from within a cgi script while running a local cgi server. The cgi script simply receives some data from Google Earth and passes it to the .py script which is currently being called using execfile('script.py') placed at the end of the cgi script.

The script runs to completion, however script.py contains some print statements that I need to be able to monitor while the process runs. Any errors in the .py print to the localhost console window and the print statements seem to be buffered.

Is there a way to send the output from the .py to another python console window while the localhost console is running?

It seems like the subprocess module should do what I need but I've only been able to send the output to a variable or a logfile. This is fine except that I need to see the print statements in real-time.

Thanks in advance

+1  A: 

You say you're launching a python script from a CGI script, but you don't specify what language the CGI script is written in. Because CGI is simply an interface, it's not clear what language the CGI script is written in. I'm going to assume python, since that makes the most sense.

What would work best would be to write your messages out to a log file. In your "script.py" file, instead of using print, you should open a log file for appending, like so:

logfile = file("/var/log/my-app-log.txt", "a")

Then, wherever you have a print statement like this:

print("Starting to do step 2...")

Change it to a logfile.write() statement like this:

logfile.write("starting to do step 2...\n")

Note that you will need to add newlines separately as file.write() does not add one for you.

Then, you can watch what's going on in your script with a command like tail:

tail -f /var/log/my-app-log.txt

This will show data as it gets appended to your log file.

Benson
You could also use log4py or some such magic. But logging is a life saver. The script launching the other might be a good place to catch and log exceptions too.
fuzzy-waffle
A: 

Use popen2 or subprocess to launch a console while redirecting your output stream to that console.

Nikhil Chelliah