MySQL and Perl for the Web: Chapter 3 Section 1 (1/3)
[next] |
Improving Performance with mod_perl
What mod_perl Is and How It Works
When Apache receives a request for a static HTML file, it can serve the request directly by opening the file and writing it out over the network to the client. This is not true for programs, such as the Perl cgi-bin
scripts we wrote in Chapter 2. Those scripts aren't processed by Apache itself. Instead, Apache starts up Perl as an external process and returns its output to the requesting client. This works well for extending Apache's capabilities, but invoking an external program taxes the Web server host and also introduces some delay into serving the request. This overhead is incurred repeatedly as script requests arrive because Apache starts up a new Perl process to handle each one.
An alternative to running scripts using external processes is to make the script handler part of Apache itself. In the case of Perl scripts, we can use the mod_perl
module to embed the Perl interpreter into Apache. The result is that Apache gains the capability to execute Perl scripts directly. This approach has several advantages:
Apache can execute Perl scripts more quickly because it need not start up or wait for standalone external processes.
A given Apache process can serve many script requests because it doesn't terminate when a script finishes; it just waits for the next request. This allows Apache to perform caching for scripts that are requested repeatedly, which results in a further performance improvement. (The script-execution process involves examining the script and compiling it to an internal form, and then running that form. When Perl runs as part of Apache, the compiled script remains loaded in memory and is immediately available for execution. It need not be recompiled if the server receives another request for it.)
Because the Perl interpreter doesn't just exit when the script terminates the way it does when Perl is executed as a standalone process, the script-execution environment persists across scripts. This makes possible some things that can't be done when scripts are executed individually by independent Perl processes. One of these is persistent database connections (connections to a database server that can be shared over successive scripts, minimizing overhead for setup and teardown).
[next] |
Created: June 26, 2001
Revised: June 26, 2001