Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Running Long process thru CGI

by sara2005 (Scribe)
on Jun 14, 2006 at 01:40 UTC ( [id://555145] : perlquestion . print w/replies, xml ) Need Help??

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

Hi Monks,

Is there any other approach or example other than the one provided by merlyn for running long process thru CGI?

The above approach described in the column by merlyn works perfectly but the reason I posted this question is

1. I am confused as to how to use a portion of the code as a subroutine. i.e. I have a need to run long processes in all my cgi programs and I would like to call this as a subroutine by passing the commands I want to execute. for example, if I run validate_login.cgi, I want to call the subroutine (say run_commands()), execute and complete the long process, return back to validate_login.cgi and then redirect to the next page display_menu.cgi. The same process will be followed for other pages as well. Hence, I am looking to see if there are any examples on those lines.

2. I am also thinking of having watching long process by meryln as a separate cgi (say 'run_long_process.cgi') rather than subroutine so that each of the cgi programs I write could be redirected to the 'run_long_process.cgi' to execute the commands. But then I was not sure how to to redirect to the appropriate cgi based on what page I came from? (i.e. if I came from 'page1.cgi', after running the commands in 'run_long_process.cgi', it should be redirected to 'page2.cgi'. If I started from 'page3.cgi', it should be redirected to 'page4.cgi'. Do I have to hardcode this part in run_long_process.cgi?)

Replies are listed 'Best First'.
Re: Running Long process thru CGI
by sgifford (Prior) on Jun 14, 2006 at 03:50 UTC
    I did this in my CGIBurn program; feel free to steal some of that code. It was written before Merlyn's column, but does basically the same thing.
Re: Running Long process thru CGI
by rodion (Chaplain) on Jun 14, 2006 at 04:17 UTC
    Under Apache, scripts with a name that starts with "nph-" are handled as no-parse-header scripts. Apache doesn't wait to process their output, but rather sends it directly through from your script as you produce it. This was the clasic way of showing activity from a script that produced output over a long period.

    The cost, besides the odd name prefix, was that you had to supply all the headers. As I remember, all you need is to output two additional lines at the beginning:

    Content Type: text/html [blank line]
    (The blank line is important.)

    I'm not at a computer where I can check this, but I'm sure someone else will add a correction if I remember it wrong, and I can check it in the morning.

    I checked this morning, the headers are all provided by Use

    $q = new CGI; print $q-header();
Re: Running Long process thru CGI
by merlyn (Sage) on Jun 14, 2006 at 03:39 UTC
Re: Running Long process thru CGI
by gam3 (Curate) on Jun 14, 2006 at 02:52 UTC
    You might want to look into AJAX.
    -- gam3
    A picture is worth a thousand words, but takes 200K.