in reply to Re^2: Running CGI script within another cgi script
in thread Running CGI script within another cgi script

You need to run the long running function in the background, so the CGI script will terminate right away. This way, the <meta http-equiv="refresh" ... will also be sent right away, independently of any buffering that might happen on the way from the CGI script/webserver to the browser... and the browser will then request some-html-file after the specifed period (which, btw, should be at least 4 minutes (if the function call takes that long), not 2 secs...).

In case the webserver is Apache running on something unix-ish, you'd fork (and if needed exec) a child process to run something in the background. But don't forget to close (or redirect to file) STDOUT and STDERR in the child before you start the long running task. Otherwise Apache will wait on the other end of the pipes setup to the CGI process until both the parent CGI and its child have closed the handles — meaning the initial request would wait/hang for as long as the function call runs (STDIN doesn't need to be closed, because Apache is on the sending side of the pipe and closes itself).

Replies are listed 'Best First'.
Re^4: Running CGI script within another cgi script
by Anonymous Monk on Nov 27, 2009 at 19:47 UTC
    Hi almut, Is this the concept you are talking about ... http://www.stonehenge.com/merlyn/LinuxMag/col39.html Thanks
Re^4: Running CGI script within another cgi script
by Anonymous Monk on Nov 27, 2009 at 19:55 UTC
    Thanks almut for pointing me to the right way. One last question ... You said in *nix fork/exec the child process and the concept also does the same ... but I am working on windows perl ... will the same thing work fine in windows too. I believe it will ..

      I, too, believe it will work :)  But I don't have any first hand experience, as I've never (had to) run Apache on Windows...  I suppose Perl's fork emulation on Windows should be okay, but I'm not sure how Apache behaves in the respect in question (this is not to say it wouldn't work — I just can't tell).  In case of doubt, I would just try and see... :)

Re^4: Running CGI script within another cgi script
by Anonymous Monk on Nov 28, 2009 at 00:16 UTC
    Hi almut,

    It still expiring ... I am doing like below

    if (my $pid = fork) { # parent does #delete_all(); # clear parameters print redirect('http://localhost/test.html'); } elsif(defined $pid) {close STDERR;close STDOUT;function_call();exit 0; +} else {die message;}
    Now, also it's not redirecting and at last getting expired. How should I troubleshoot further. Please help. Thanks

      Generally looks okay to me... except for the print redirect('http://localhost/test.html'); which would redirect without delay, so that the browser would request the output page more or less immediately, when it hasn't been written yet completely (I'm supposing the test.html is being produced by function_call(), as you mentioned before).

      This would either lead to partial/premature content being returned, or - more likely on Windows (AFAIK, the file would be locked by the writing process) - the webserver would not be allowed to read it at all, before the long-running process has finished writing the file...   Why not stick with the http-equiv="refresh" (with a delay) that you had tried originally?

      BTW, are you sure it's the original request that hangs, or is it the new redirected one?

        Hi almut,

        Thanks again for your reply on this. To explain properly...below is what happening:

        Actually it supposed to do 1.redirect to test.html 2.execute the function call in chile thread 3. Write down the final result to a tmp file 4. rename the tmp file to test.html

        Instead of this it's behaving like 1.*Not* redirecting to test.html 2.executing the function call successfully. 3.creating and writing the final result to tmop file properly 4.*Not renaming* the tmp file and after some time getting expired.

        any clue what I am doping wrong again?Thanks

        also, the test.html file is getting generated after the function-call. Means, function-call is not generating the html file.

        after the function completes, it carries out some more process and then the final result gets written ....