AnonGeek
AnonGeek

Reputation: 7938

Show progress of PYTHON CGI script

I have a PYTHON CGI script(Content-type: text/plain) which takes about 10 minutes to execute. I want to see the execution status of my script on my browser .

Like below:

Part 1 of script executed...

Part 2 of script executed..

Part 3 of script executed..

Execution Complete

I am using print statements, but it is outputting all the print statements all-together only after the script has completed execution, and not one by one.

Please help ..

Upvotes: 3

Views: 2047

Answers (3)

Peter Downs
Peter Downs

Reputation: 482

Try yielding output part by part instead of returning a full response. Are you using any web framework? Web.py has a good example. As they say,

[...] you need to make sure you add the Transfer-Encoding chunked header for it to display properly. Otherwise the browser will buffer all data before displaying it to you.

If you're not using web.py or another web framework, you may try it anyway -- I don't know if it will work though.

Upvotes: 1

André Caron
André Caron

Reputation: 45224

I suspect the real blocking point is the gateway (web server), not your CGI application. The gateway technically has to validate the response and make sure it conforms to the HTTP version the gateway is using with the client.

I'm not sure the gateway is even allowed to forward the headers until all of the request is processed. If you take a look at the CGI specification under section 3.1 "Server Responsibilities", you can read the following:

The server MUST perform translations and protocol conversions on the client request data required by this specification. Furthermore, the server retains its responsibility to the client to conform to the relevant network protocol even if the CGI script fails to conform to this specification.

If the script takes a long time to run and you want periodic updates, you are much better off re-thinking your architecture. Take a look at more classic strategies for this approach, mainly having the script run in a background process which writes to a database and have an write some AJAX code to pull notifications from the server. Depending on what you're using as a server stack, you might also be able to write your application for communication over a web socket which would allow you to hold a continuous connection and send updates whenever you want.

Upvotes: 5

Donald Miner
Donald Miner

Reputation: 39903

Try doing:

import sys
...

print "Part 1 of script executed..."
sys.stdout.flush() # do this after the print

This flushes the standard out buffer, which could be the cause of one of your issues. It is also very likely that your browser or webserver might be causing this behavior, in which case solving this problem will be more difficult.


I'm not sure what you are trying to do here, but you may want to use AJAX to asynchronously fire off your script and then asynchronously load the data back. This is better behavior for browsers and webservers to handle than a cgi script taking several minutes.

Upvotes: 2

Related Questions