Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Browser Timeout

by Jonas (Beadle)
on Feb 22, 2001 at 20:38 UTC ( #60239=perlquestion: print w/replies, xml ) Need Help??

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

I don't know if this is really a Perl question, but here it goes anyway.

Is there a way to change the amount of time the browser waits for response from a page/script? I think right now most are set at about 2-3 minutes until they give you the "Couldn't find" message.

I'm needing it to change this because one of the scripts on my site does some major data crunching for all data the users have entered that day. It could take 45 seconds, or it could take 5 minutes. It just depends on the amount of work done in the web tool. I'm needing this so in the middle of the script call it doesn't crap out and close the connection to our datawarehouse.

setTimeout won't work because the time varies and I don't want the tool to wait for 5 minutes no matter the amount of data changed. Doing a redirect won't work either due to security issues.

Thanks in advance,

Replies are listed 'Best First'.
Re: Browser Timeout
by merlyn (Sage) on Feb 22, 2001 at 20:43 UTC
Re: Browser Timeout
by bastard (Hermit) on Feb 22, 2001 at 21:14 UTC
    Look into nph scripts. (non-parsed header) Basically you get to bypass the webserver (and it's output buffering) and talk directly to the browser via the cgi. The also permits real-time display output to be sent to the browser. We have used it for search engine indexing of sites before (indexes have taken as long as 10 minutes.)

    I'm not totally sure of the details, but as long as you periodically return data the connection should be held open. IIRC you need a nph script to do this. Try returning a "." every so often. (This also lets the user know something is happening).

    Look in the docs for the section titled "USING NPH SCRIPTS"

      I'm curious because I've never used them, but what's the added advantage or utility of an NPH script over a non-NPH script. To bypass output buffer and avoid browser timeouts, I usually just set $&#124 and an alarm:

      { local $| = 1; local $SIG{ALRM} = sub { print `/usr/bin/fortune`; alarm ALARM_TIME; + }; alarm 1; ... process that (might) take a very long time ... alarm 0; }

      Would using NPH be better?

      update Thanks, merlyn.

      Thanks, ERic

        This works under modern Apache, but under older Apache versions, the script also had to be named nph-something to turn off the buffering. Consider the name as a signal that Apache should do its own $| = 1 equivalent.

        The other things that older Apache required was that these "NPH" scripts provide the first HTTP status line as well, since Apache essentially stepped out of the way entirely. I think this is still true for scripts named like this as a backward compatiblity.

        -- Randal L. Schwartz, Perl hacker

        I have the same question.... I have several scripts that take longer than my browser has patience. Where exactly do I PUT the ' $|=1; ' in order to make sure that I don't get timed out?
      Thanks guys, you've given me more than enough to work with... j
Re: Browser Timeout
by Rhandom (Curate) on Jun 13, 2001 at 19:24 UTC
    There is another way. (Probably easier to). Apache and the browser won't time out as long as data is being sent. So send some data...

    #!/usr/bin/perl $| = 1; # thousands of ways to do this print "Content-type: text/html\n\n"; ### set up an alarm my $timeout = 4*60; # apache normally times out after 5 min print "<!--\n"; # hide output $SIG{ALRM} = sub{ print ".\n"; alarm($timeout); }; # the timeout will send something to the browser every # now and then to keep it from timing out alarm($timeout); &do_something_heinous(); alarm(0); print "-->\n"; # don't need to hide output anymore print "All done\n"; exit; sub do_something_heinous { sleep(6 * 60); }

    My company actually has a module that we can use to wrap around this. It works great. (Actually, I typed this code in from memory -- our servers are down from a storm -- hope it compiles, but the idea should be intact).

    my @a=qw(random brilliant braindead); print $a[rand(@a)];

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://60239]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2020-10-29 23:14 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (274 votes). Check out past polls.