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

Can anybody offer guidance so i can understand something about mod_perl? I put this example together in hopes that it would illustrate where i'm fuzzy about mod_perl, modules and consistent memory. Here's some background. Modules are brand new to me, i have only written a few in local testing. Mod_perl has been on my server for roughly 12 hours. It has been successfully tested. $ENV{'MOD_PERL'} returns (mod_perl/1.23). And a few benchmarks have made me smile.

I want to explore the abilities of mod_perl and sharing. Under mod_perl, i wanted to call ModCounter::TotalRun and return a incremental number which would represent the number of times that call was made. Without mod_perl, my ModCounter::TotalRun always returns 1. I expected that.
With mod_perl, i get a one digit number that changes randomly (random = "jameshasnoclue"). I read about this in the mod_perl documentation. And it explained how to stop the randomness from occuring and bring the result back to a consistent 1.

But i want the opposite. I want the counter to grow every time ModCounter::TotalRun is called. I was hoping that i could then connect several perl scripts on my server to ModCounter.pm. Each script could call the module ModCounter::TotalRun numerous times and increment the counter.

Here's the attempted module: ModCounter.pm
package ModCounter; use Exporter; @ISA = qw(Exporter); use vars qw($cnt); @EXPORT_OK = qw(TotalRun); sub TotalRun{ $cnt++; return $cnt; } 1;
And here's the script which calls ModCounter.pm.
#!/usr/bin/perl -w use strict; print "Content-type:text/html\n\n"; use ModCounter; print "cnt=".ModCounter::TotalRun; exit;

I was hoping to get an incremental count when it runs in mod_perl. Thanks for monkin.
jtrue

Replies are listed 'Best First'.
Re: Persistent memory with mod_perl and a module.
by sauoq (Abbot) on Oct 19, 2002 at 00:21 UTC
    With mod_perl, i get a one digit number that changes randomly (random = "jameshasnoclue"). I read about this in the mod_perl documentation.

    If you read about it then you understand that Apache forks servers to handle requests and that each process has its own embedded perl interpreter and each intepreter has its own copy of the variable. So, if you want them to all share and/or modify the same value, then you need to store the value outside of the process and make sure that all of the processes update it in an orderly manner.

    A common way to do that is to put the value in a file and then have your script use voluntary locking for access to it. If this is primarily a learning experience, it would probably be worthwhile for you to implement that. If you just want to get it working, there is likely to be a module available on CPAN that would help, though I don't know which.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Persistent memory with mod_perl and a module.
by sharkey (Scribe) on Oct 19, 2002 at 17:17 UTC
    As noted above, each apache child process has its own copy of the ModCounter package and $cnt variable. A couple suggestions:

    1) Store the counter in a database. This is often the easiest method if your script is database driven anyway.

    2) Use one of the shared memory modules from CPAN.

      With the exception of IPC::MM, those shared memory modules are very slow. You would be better off using MLDBM::Sync or Cache::FileCache.
Re: Persistent memory with mod_perl and a module.
by true (Pilgrim) on Oct 22, 2002 at 01:42 UTC
    thanks tons for everybody's insight on this.
    I was hoping there might be a way to support a "shared" environment. I have heard of FastCGI. Perhaps there's something in there that could help.
    IMHO the goal of so many web-based developers is to create an island between the islands as it were. The two islands of the server and the client. A project i am working on has a lot of talk about "Sharing", the web application server, and memory-based environments. The strategy, a flotilla of memory for passing requests. Smells to me like something i could be heading for in the future. Anyway, i hope my example demonstrated what i was after. Thanks again perl monks.

    Added: Lovely thread on FastCGI 206823
Re: Persistent memory with mod_perl and a module.
by true (Pilgrim) on Oct 28, 2002 at 03:59 UTC
    Yeah, i love perl. This persistent memory can be achieved with mod_perl. The key is calling the module in your Apache conf file. That's all it takes. I couldn't believe how easy it was. Now when you call the counter you get incremental values.
    PerlRequire /your/mod/perl/ModCounter.pm
    updateAfter my counter reached 101 it reset to 1. ;)