Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

mod_perl reloading handler on each call

by BigLug (Chaplain)
on Jan 10, 2005 at 02:21 UTC ( [id://420790]=perlquestion: print w/replies, xml ) Need Help??

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

I have just started getting into mod_perl (v2) and I need some help. I'm not getting the expected behaviour from the script that follows.

My module is loading and executing as I'd expect, however it's reloading upon each call from the browser. AFAIK, mod_perl is supposed to load the module once when Apache starts up, and then it should call the handler() once for each request from a browser.

This was the impression that I got from what I've been able to read. However I could be wrong. Am I?

If I'm right, please take a look at my code and let me know where I went wrong. Let me know why it reloads my script every time I call the directory in which it sits.

My .htaccess says:
SetHandler perl-script PerlResponseHandler Some::Module
My script is as follows. The warn() line is there so I can see from the logs (tail -f) when the script it reloaded:
package Some::Module; use strict; use warnings; use CGI; use Apache::RequestRec (); use Apache::RequestIO (); use Apache::Const -compile => qw(OK); # Log the loading of this handler warn("Reloading the handler.\n"); # Code snipped to create a DBI object # Code snipped to create a Cache::File object # Code snipped to create a HTML::Template object #--------------------------------------------------------------------- +- # Master apache query handler #--------------------------------------------------------------------- +- sub handler { my $r = shift; my $c = new CGI; $r->content_type('text/plain'); print $c->param('output'); return Apache::OK; }
P.S. Have I done anything that is against the mod_perl recommended way to do it?

Cheers!
Rick
If this is a root node: Before responding, please ensure your clue bit is set.
If this is a reply: This is a discussion group, not a helpdesk ... If the discussion happens to answer a question you've asked, that's incidental.

Replies are listed 'Best First'.
Re: mod_perl reloading handler on each call
by perrin (Chancellor) on Jan 10, 2005 at 03:32 UTC
    It's not reloading your module, but rather loading it once in each process. After that it stays in memory until another new process is spawned. You should load the module in your startup.pl to avoid this. It will work fine as is, but you will save some memory by loading it before apache forks.
      You are right! Thankyou for restoring my sanity!

      I changed the code an introduced a global $output then inside handler() I set $output ||= $c->param('output');. Upon each run I got the script to output $output.

      Then I kept calling the script with a different output: output=1, output=2 etc ... once I got to 9, the printed result was 'Output: 1' ... which means it had got to the end of the available processes and reused the first one again :))

      Thanks again!


      Cheers!
      Rick
      If this is a root node: Before responding, please ensure your clue bit is set.
      If this is a reply: This is a discussion group, not a helpdesk ... If the discussion happens to answer a question you've asked, that's incidental.
        You can see what's happening more simply by just printing the process id, $$, in your warning.
Re: mod_perl reloading handler on each call
by borisz (Canon) on Jan 10, 2005 at 02:39 UTC
    Try this in your httpd.conf:
    <Location /> Set-Handler perl-script PerlHandler +Some::Module </Location>
    You could use Apache::Request to parse your query.
    Boris
      Exactly the same .. the log keeps showing that it's been reloaded ..

      (On the other note, should I be using Apache::Request rather than CGI? Is that best practice in mp2?)


      Cheers!
      Rick
      If this is a root node: Before responding, please ensure your clue bit is set.
      If this is a reply: This is a discussion group, not a helpdesk ... If the discussion happens to answer a question you've asked, that's incidental.
        Are you really sure? Or did you just use Different fork's of apache?
        Try to start apache with -X an watch your logs again. You should see only one load count.
        Apache::Request is faster.
      Does Apache::Request exist for mod_perl2? I tried installing it via CPAN.pm on my box with mod_perl2 installed and I got the following:
      CPAN.pm: Going to build S/ST/STAS/libapreq-1.33.tar.gz mod_perl 1.x ( < 1.99) is required at Makefile.PL line 34.
      Is there another way to get it?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://420790]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-26 00:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found