busymeister has asked for the wisdom of the Perl Monks concerning the following question:
I am running an Apache/2.2.17 (Ubuntu) with mod_perl 2.0 and Perl version 5.10.1. I created a module CitySearcher.pm that has a handler method to handle the request from the browser. The module has roughly the following format (simplified):
package CitySearcher; use strict; ... use Tree::Trie; use threads::shared; use APR::OS; my $TRIE :shared; my $TRIE_LOCK :shared = {}; sub handler { my $r = shift; $r->content_type('text/html'); ... my $request = new CGI; my $tid = APR::OS::current_thread_id(); lock($TRIE_LOCK); if(!defined($TRIE)) { print stream "$$-$tid: creating trie\n"; my $trie = new Tree::Trie; ... #populate $trie ... $TRIE = shared_clone($trie); } print stream "$$-$tid: returning trie\n"; ... #use trie ... return Apache2::Const::OK; } 1;
Then I restart the server and pound the server with about 25 calls within milliseconds to the page. Here is the ouput from the stream:
10320-3056253808: creating trie 10320-3056253808: returning trie 10320-3064646512: returning trie 10320-3056253808: returning trie 10320-3064646512: returning trie 10320-3064646512: returning trie 10320-3056253808: returning trie 10320-3064646512: returning trie 10320-3056253808: returning trie 10320-3064646512: returning trie 10320-3064646512: returning trie 10320-3022682992: creating trie 10320-3022682992: returning trie 10320-3047861104: creating trie 10320-3047861104: returning trie 10320-3056253808: returning trie 10320-3056253808: returning trie 10320-3022682992: returning trie 10320-3064646512: returning trie 10320-3031075696: creating trie 10320-3031075696: returning trie 10320-3064646512: returning trie 10320-3022682992: returning trie 10320-3056253808: returning trie 10320-3056253808: returning trie 10320-3039468400: creating trie 10320-3039468400: returning trie 10320-3022682992: returning trie 10320-3064646512: returning trie 10320-3056253808: returning trie
As you can see, thread 3056253808 initially creates the trie and then thread 3064646512 uses it too (no creating trie output). A little bit later thread 3022682992 comes along and creates a trie again. Why was the trie not shared? All threads are clearly running in the same process.
Thank you very much for your help.
Sincerely,
Pawel
SOLVED (I think):
I removed PerlInitHandler Apache2::Reload from my Apache configuration. Looks like the shared variables are removed when the Apache2::Reload module does its thing. I hope this tip helps anyone with a similar problem in the future.
|
|---|