print "Content-type:text/html\n\n $output";
[some time-consuming task here]
Perl waits until the time-consuming task is done before showing the output. (And tThere are no more print commands after the time-consuming task by the way.)
I thought I'd get clever and put the time-consuming task into an external file, but that doesn't help:
print "Content-type:text/html\n\n $output";
`perl timeConsumingTask.pl`; # OR
do('timeConsumingTask.pl'); # OR
system('timeConsumingTask.pl');
There's no difference between these. Perl still waits until the external script has run before showing the output. (By the way, I can't try 'exec', because my webhost has disabled it for security reasons.)
I thought I had a clever solution, by redirecting the user to a new page before running the time-consuming task, but that doesn't help either:
open(FILE,'>output.html');
print FILE $output;
close(FILE);
print "Location:http://example.com/output.html\n\n";
do('timeConsumingTask.pl');
Same deal. The time-consuming task runs first before showing the output.
My final idea is to set a flag in a text file along with a list of data to process, and have a cron job run every minute, checking for the flagged file, and then running if it finds the flagged file. But this is really inelegant. I'd rather not have a cron job running every single minute of every single day, and even if it does, it doesn't process the data as quickly as a regular script would. (The data takes about six seconds to process, and when the user goes to the next page, the processed data might not be ready yet. But please please please, let's not get sidetracked about that. Please trust me that the issue above is exactly as I've described it.)
have you tried closing the STDOUT output stream?
close (STDOUT);
you may have to run this script using NonParsed Headers [webmasterworld.com].
edit: grammar
[edited by: phranque at 8:56 pm (utc) on Sep. 4, 2009]
if ($pid = fork) {
## Parent process, return response to browser
print "Content-type:text/html\n\n $output";
}
else {
## Child process
close (STDOUT);
## [time consuming function]
}
Fork allows heavyweights to process as background processes, allowing the parent process to return an immediate response to the browser.