stickman has asked for the wisdom of the Perl Monks concerning the following question:

I have a cgi script that runs and produces output to a web page. The process runs about 20 minutes and continues to flush the output as it executes. What I would like to do is have a url people can go to and see the output as it is happening and not actually invoke the cgi script. Kinda like turning your TV on and watching the 12:30 - 1:00 show. The show is running and anybody can tune in and watch between those times. In my case though I would like the entire show to be seen as well as any new output produced all the way until it finishes. Does anybody have any ideas how I might accomplish this? TIA..
  • Comment on Stream or Broadcast Output from CGI script

Replies are listed 'Best First'.
Re: Stream or Broadcast Output from CGI script
by jethro (Monsignor) on Apr 02, 2009 at 03:29 UTC
Re: Stream or Broadcast Output from CGI script
by dsheroh (Monsignor) on Apr 02, 2009 at 10:29 UTC
    What I would do for something like that is:

    1) Write a program which actually does the processing. This program should be completely independent of the CGI/web environment and able to be run from the command line, from cron, or from anywhere else. Output is written to either a database or a text file, depending on its complexity and what needs to be done with it.

    2) Write a CGI interface which, when called, checks to see whether the program from #1 is already running and starts it up - still running as a separate, non-CGI process - if it isn't. Once the #1 program is running, grab its output and display it as a web page.

    Putting the actual processing activity outside of the CGI environment is important if you want the person who originally starts the processing to be able to close their browser without aborting any remaining processing. It also has the side benefit that, if (when...) you someday want it to just run automatically whether anyone is watching or not, then you can set up a cron job to do just that instead of requiring someone to manually initiate it and then wait for it to complete.

Re: Stream or Broadcast Output from CGI script
by locked_user sundialsvc4 (Abbot) on Apr 02, 2009 at 12:42 UTC

    For “a process that runs for 20 minutes,” you might wish to use a batch-processing monitor of some kind in order to manage that workload – in particular, to make sure that multiple copies of it aren't running at once.

    The “I‘m working on it” example from Randall Schwartz, hotlinked above, might be most appropriate in this situation. The user might wish to spend the twenty minutes poking around on perlmonks.org in the meantime, for instance...