Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Keeping browsers alive for long lived CGIs

by Felonious (Chaplain)
on Apr 27, 2002 at 19:05 UTC ( #162547=snippet: print w/replies, xml ) Need Help??
Description: If you've ever needed to add a CGI interaface to a long running process, you probabaly know that if you don't provide the browser with periodic "feedback", it will time out and the user will never see the result. The code below solves this by forking and having the parent send back NULLs (\0) to the browser until the child process completes. There's nothing magic about Null except that it's doesn't display. If you want a "progress indicator" effect, you could replace them with "*" or whatever you like (though some browsers may not display them until the script completes).

Update: The code below turned out to be responsible for some strangeness in Apache under rare circumstances, yet I could never find the real root of it nor a fix. Another solution involving alarms was deployed and has worked consistantly without problem, I'll post that whenever I find the time.
#!/usr/bin/perl -w
use strict;
use POSIX;

# incomplete example, tailor to you needs

$| = 1;

my $pid = fork;
my $result;
if ($pid) {
        my $kid;
        print "Please Wait...<br>\n";
        do {
                sleep 2;
                print "\0";
                $kid = waitpid(-1, &WNOHANG);
                if ($kid > 0) {
                        $result = $? >> 8;
        } until ($kid == -1);
} else {
        if (defined $pid) {
                CORE::exit 1;
        } else {
                print "Failed to fork!<br>";
                $result = 1;

if (not $result) {
        print "Run successful<br>\n";
} else {
        print "Run Failed. Error($result)<br>\n";

Replies are listed 'Best First'.
•Re: Keeping browsers alive for long lived CGIs
by merlyn (Sage) on Apr 30, 2002 at 06:25 UTC
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://162547]
marioroy At a large firm, had to awaken a security guy a few times so that nobody would catch him sleeping.
marioroy On a large dairy farm, almost went off the road with a 10 wheeler from exhaustion. On the farm, almost went off the silo with a big tractor. At the Fransiscan monastery, almost slipped off the dome while painting it.
[marioroy]: In Saudi Arabia caught fire but didn't burn. I think angels exist.
Lady_Aleena clean up after dinner finally.
[Discipulus]: uch! so we are twice lucky to have MCE..;=)
[marioroy]: Working a midnight graveyard shift, on the way home ran out of gas. just 2 minutes later somebody stops with a small tank of gas. It's wierd. There's always someone nearby for some reason.

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2017-05-29 08:22 GMT
Find Nodes?
    Voting Booth?