2

I have a PERL script in cgi-bin and delivered via apache that creates a PDF file from a set of XML files. Long story short... it takes a little while and my browser reports a 504 Gateway Time-out error before it completes.

I don't want to increase the default timeout in my apache config, but I do want to increase the timeout for the sake of this one script which is accessible only by admins who are properly authenticated.

Is there a command I can add to the PERL cgi script that will increase the timeout?

In PHP there are the commands set_time_limit($n) and ini_set('max_execution_time', $n); I'm looking for the PERL equivalent if there is one.

Octopus
  • 8,075
  • 5
  • 46
  • 66
  • 1
    PHP's `set_time_limit` and `max_execution_time` do not override your webserver's timeout setting. If Apache determines a script has been running for too long, it will time out regardless. – ThisSuitIsBlackNot Jan 22 '14 at 15:56

2 Answers2

4

It isn't Perl that's timing-out, it is your HTTP server (e.g Apache).

Present conventional wisdom is that long-running jobs should be queued, processed by worker jobs (that do not live inside the web application logic, and then the client re-polls for the results at a later time.

Folks use this model for short running jobs, in order to increase web app throughput, but you only really have three choices if your job exceeds the server time out:

  1. Make the jobs shorter
  2. Use the queue + alert/repoll method
  3. Change the server timeout.

-------------------------------- 2014-12-10 edits -----------------------------

To increase the gateway timeout, on apache 2.4 see the docs for mod_reqtimeout

Len Jaffe
  • 3,442
  • 1
  • 21
  • 28
  • 1
    Your information may be valid for a particular scenario, but in this one I asked a specific question. This isn't a case of finding alternatives. – Octopus Jan 22 '14 at 17:06
  • 1
    Perhaps you misunderstand. You asked how to make the Perl timeout longer, and I explained that you can't because it isn't Perl that is timing out. – Len Jaffe Jan 23 '14 at 18:44
  • How do you increase the timeout in Apache? – Jon Dec 10 '14 at 09:49
0
use threads;
sub keepalive_httpd{
    $SIG{'KILL'} = sub { threads->exit(); };
    $| = 1;
    do{
        print ".\n";
        sleep 1;
    } while(1);
}

my $thr = threads->create('keepalive_httpd');

do something long...

$thr->kill('KILL')->detach();
print "<BR>\n";
  • how do we solve this in CGI while printing html? Inside html I have to print large number of elements. When I use this excellent solution I can solve the gateway timeout issues but output is always in text format instead of html – kranthi guttikonda Sep 22 '22 at 20:11