Re: shared variable between PerlChildInitHandler and PerlResponseHandler

Torsten Förtsch <[email protected]>
Newsgroups gmane.comp.apache.mod-perl
Message-ID <[email protected]>
On Thursday, August 19, 2010 18:24:15 Mark Risher wrote:
> > The only thing where mod_perl may stay in the way here is if you use
> > somehow a 
> > 
> > different interpreter for child_init than for handler. Do you use a
> > threaded  MPM, e.g. windows? Do you use the +Parent PerlOption in a
> > VHost?
> 
> [mr] I haven't changed the MPM, and I'm running on a Linux host. I do not
> have  the +Parent option

httpd -V ?

could you post the httpd.conf?

could you post the complete code (as simplified as possible)?

As an example I have just added the following lines to my httpd.conf:

<Perl>
package My::XX;

use strict;
use Apache2::RequestRec ();
use Apache2::RequestIO ();

my $var;

sub pv {
    my ($prefix)=@_;
    warn "$prefix: var=".(defined $var ? "'$var'" : "UNDEF");
}

sub Init {
    pv 'Init before';
    $var++;
    pv 'Init after';
    0;
}

sub Response {
    my ($r)=@_;

    $r->content_type('text/plain');

    pv 'Response before';
    $var++;
    pv 'Response after';

    $r->print("$var\n");
    0;
}
</Perl>

PerlChildInitHandler My::XX::Init
<Location /My/XX>
  SetHandler modperl
  PerlResponseHandler My::XX::Response
</Location>

After a restart I see these lines in the error_log:

Init before: var=UNDEF at /etc/opt/apache-prefork/httpd.conf line 442.
Init after: var='1' at /etc/opt/apache-prefork/httpd.conf line 442.
Init before: var=UNDEF at /etc/opt/apache-prefork/httpd.conf line 442.
Init after: var='1' at /etc/opt/apache-prefork/httpd.conf line 442.
Init before: var=UNDEF at /etc/opt/apache-prefork/httpd.conf line 442.
Init after: var='1' at /etc/opt/apache-prefork/httpd.conf line 442.
Init before: var=UNDEF at /etc/opt/apache-prefork/httpd.conf line 442.
Init after: var='1' at /etc/opt/apache-prefork/httpd.conf line 442.
Init before: var=UNDEF at /etc/opt/apache-prefork/httpd.conf line 442.
Init after: var='1' at /etc/opt/apache-prefork/httpd.conf line 442.

and each "curl http://localhost/My/XX" produces a pair of these:

Response before: var='1' at /etc/opt/apache-prefork/httpd.conf line 442.
Response after: var='2' at /etc/opt/apache-prefork/httpd.conf line 442.
Response before: var='1' at /etc/opt/apache-prefork/httpd.conf line 442.
Init before: var=UNDEF at /etc/opt/apache-prefork/httpd.conf line 442.
Response after: var='2' at /etc/opt/apache-prefork/httpd.conf line 442.
Init after: var='1' at /etc/opt/apache-prefork/httpd.conf line 442.
Response before: var='1' at /etc/opt/apache-prefork/httpd.conf line 442.
Response after: var='2' at /etc/opt/apache-prefork/httpd.conf line 442.
Response before: var='1' at /etc/opt/apache-prefork/httpd.conf line 442.
Response after: var='2' at /etc/opt/apache-prefork/httpd.conf line 442.
Response before: var='1' at /etc/opt/apache-prefork/httpd.conf line 442.
Response after: var='2' at /etc/opt/apache-prefork/httpd.conf line 442.
Response before: var='2' at /etc/opt/apache-prefork/httpd.conf line 442.
Response after: var='3' at /etc/opt/apache-prefork/httpd.conf line 442.
Response before: var='1' at /etc/opt/apache-prefork/httpd.conf line 442.
Response after: var='2' at /etc/opt/apache-prefork/httpd.conf line 442.
Response before: var='2' at /etc/opt/apache-prefork/httpd.conf line 442.
Response after: var='3' at /etc/opt/apache-prefork/httpd.conf line 442.
Response before: var='2' at /etc/opt/apache-prefork/httpd.conf line 442.
Response after: var='3' at /etc/opt/apache-prefork/httpd.conf line 442.

You see $var gets incremented and it preserves its state between calls. Why do 
I see multiple occurrences of var=2? There are multiple apache instances 
active. Each one has its own perl interpreter. So I have several interpreters 
and hence several $var instances that are incremented independently.

Modify pv() to print out the process ID and you can see it:

sub pv {
    my ($prefix)=@_;
    warn "$$ $prefix: var=".(defined $var ? "'$var'" : "UNDEF");
}

Torsten Förtsch

-- 
Need professional modperl support? Hire me! (http://foertsch.name)

Like fantasy? http://kabatinte.net
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.