Can I use SpeedyCGI to speed up Perl scripts?

We have the SpeedyCGI software installed on our servers, and we encourage Perl programmers to use it to speed up their scripts.

On this page:

What is SpeedyCGI?

SpeedyCGI is a Perl “wrapper” that avoids the overhead of starting Perl from scratch for each new page request.

Avoiding that overhead makes a significant difference in speed for large scripts. A complicated Perl script can easily handle dozens of page requests per second using SpeedyCGI. The page you’re viewing is part of a custom Perl-based content management system that uses SpeedyCGI to ensure good performance.

How do I use it?

The basic change is simply to replace the “#!/usr/bin/perl” line at the top of a working Perl script with:

#!/usr/bin/speedy

The SpeedyCGI documentation pages have more details.

Are there any SpeedyCGI restrictions on your servers?

You need to make sure your script doesn’t just stay running forever. If the script runs for more than a couple of hours, our systems may “kill” it, which could interrupt it in the middle of a request.

SpeedyCGI will automatically quit a script if it doesn’t receive any requests for an hour, or if it has received more than 500 requests. However, a script might keep running for hours if it receives a slow trickle of requests (like one per minute).

You can solve this by adding the following code to your script:

use CGI::SpeedyCGI;
use vars qw($script_start_time);

my $sp = CGI::SpeedyCGI->new;
if ($sp->i_am_speedy)
{
    # Remember when the script started
    unless (defined $script_start_time)
    {
        $script_start_time = time;
    }
    
    # If we've been running for an hour, quit after this request
    if (time - $script_start_time > 3600)
    {
        $sp->shutdown_next_time;
    }
}

This code will guarantee that SpeedyCGI runs for no more than two hours. That’s because after your script has been running for an hour, one of the following will happen: