Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I display real-time python script output on a website?

I have a Python script that outputs something every second or two, but takes a long while to finish completely. I want to set up a website such that someone can directly invoke the script, and the output is sent to the screen while the script is running.

I don't want the user to wait until the script finishes completely, because then all the output is displayed at once. I also tried that, and the connection always times out.

I don't know what this process is called, what terms I'm looking for, and what I need to use. CGI? Ajax? Need some serious guidance here, thanks!

If it matters, I plan to use Nginx as the webserver.

like image 378
Lin Avatar asked Aug 31 '25 23:08

Lin


2 Answers

First of all - your script must output header:

Connection: Keep-Alive

Because browser must know that it will have to wait.

And your script must output data without buffering. And stackoverflow has already answered this question.

like image 145
Oduvan Avatar answered Sep 03 '25 11:09

Oduvan


The solution is to flush the output buffer at select points in the script's execution - I've only ever done this in PHP via flush() but this looks like the Python equivalent:

cgiprint() also flushes the output buffer using sys.stdout.flush(). Most servers buffer the output of scripts until it's completed. For long running scripts, 8 buffering output may frustrate your user, who'll wonder what's happening. You can either regularly flush your buffer, or run Python in unbuffered mode. The command-line option to do this is -u, which you can specify as #!/usr/bin/python -u in your shebang line.

like image 34
Ben Regenspan Avatar answered Sep 03 '25 13:09

Ben Regenspan