For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2007 > Global variable - No of hits counter









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Global variable - No of hits counter
Tom Tom

2007-01-22, 8:02 am

Hi,

In my mod_perl (Authentication module). I have global
variable defined as follows

use vars qw( $SESSION_CLEANUP_COUNTER);
$SESSION_CLEANUP_COUNTER=0;


my intention is to count no of times it is getting
executed by clients (no of hits). I am incrementing it
within Authenticate method as follows.
$SESSION_CLEANUP_COUNTER ++,

But everytime it prints 1.

How can I get this going,




________________________________________
________________________________________
____
Looking for earth-friendly autos?
Browse Top Cars by "Green Rating" at Yahoo! Autos' Green Center.
http://autos.yahoo.com/green_center/
Mumia W.

2007-01-22, 8:02 am

On 01/21/2007 04:55 PM, tom tom wrote:
> Hi,
>
> In my mod_perl (Authentication module). I have global
> variable defined as follows
>
> use vars qw( $SESSION_CLEANUP_COUNTER);
> $SESSION_CLEANUP_COUNTER=0;
>
>
> my intention is to count no of times it is getting
> executed by clients (no of hits). I am incrementing it
> within Authenticate method as follows.
> $SESSION_CLEANUP_COUNTER ++,
>
> But everytime it prints 1.
>
> How can I get this going,
>


I am not an expert in mod_perl.

Mod_perl is good with making program loading more efficient, but my
experience has been that it's not very good a storing
constantly-changing data such as the counter you created.

Apache starts several processes to handle requests, and mod_perl is
active in each process, but each process has its own data, and you can't
guarantee that the same process that handles *this* request will handle
the *next* request.

For example, take these four processes:
USER PID PROGRAM
root 5105 apache2
www-data 5106 apache2
www-data 5107 apache2
www-data 5108 apache2

Any requests will be served by either pid 5106 or 5107 or 5108. If 5106
gets the first HTTP request, the instance of mod_perl in 5106 will set
$SESSION_CLEANUP_COUNTER in 5106, but 5107 and 5108 will be unaffected.

If the next request goes to 5107, $SESSION_CLEANUP_COUNTER will start
off with an "undef" value. If you keep quickly hitting "refresh" on the
page, you should eventually see some incrementing--but it will be uneven
because each apache2 process will have a different
$SESSION_CLEANUP_COUNTER, and the assigning of HTTP requests to apache
sub-processes seems to be semi-random.

Mod_perl is great for making static data and module procedures more
efficient; however, I think that data that *must* be updated in a linear
fashion yet shared between several apache processes should be stored
externally--in a database or a file (on ramdisk?) or in IPC.

Again, this is my €0.0002 from my limited experience with mod_perl and a
lot of deduction.

Well perhaps there was a little induction too: try this program to prove
to yourself that each mod_perl process has a different copy of the $sc
variable:

use strict;
use warnings;
our $sc;
$sc++;

print "Content-Type: text/html\n";
print "\n";

print qq{
<title> Session Cleanup Test </title>

<p> This is a test: $sc . <br>
PID: $$
</p>

};

__END__

Keep hitting refresh. So long as the PID stays the same, $sc increments
normally, but when apache assigns a new sub-process to deal with
requests, $sc restarts at the beginning (1).

Tom Tom

2007-01-22, 6:58 pm

Hi,

My intention was to write a cleanup routine when the
server encounters that many requests,

but somehow I achieve it via a CRON job. which
executes a perl script (CGI).

Can I know how to pass parameters from the CRON job
e.g
cleanup folder and delta time in seconds and
thereafter retreive that from the perl.

My cleanup rountine currently hardcoded with these
values it looks as follows #!/usr/bin/perl

I need to get the temp_dir and delta from the CRON job

##
##
#use strict;
use warnings;
use CGI::Session::ExpireSessions;

CGI::Session::ExpireSessions -> new(temp_dir =>
'/home/lalitha/mod_cas/sessions',delta=>120, verbose
=> 0) -> expire_file_sessions();





--- "Mumia W." <mumia.w.18.spam+nospam@earthlink.net>
wrote:

> On 01/21/2007 04:55 PM, tom tom wrote:
> global
> incrementing it
>
> I am not an expert in mod_perl.
>
> Mod_perl is good with making program loading more
> efficient, but my
> experience has been that it's not very good a
> storing
> constantly-changing data such as the counter you
> created.
>
> apache starts several processes to handle requests,
> and mod_perl is
> active in each process, but each process has its own
> data, and you can't
> guarantee that the same process that handles *this*
> request will handle
> the *next* request.
>
> For example, take these four processes:
> USER PID PROGRAM
> root 5105 apache2
> www-data 5106 apache2
> www-data 5107 apache2
> www-data 5108 apache2
>
> Any requests will be served by either pid 5106 or
> 5107 or 5108. If 5106
> gets the first HTTP request, the instance of
> mod_perl in 5106 will set
> $SESSION_CLEANUP_COUNTER in 5106, but 5107 and 5108
> will be unaffected.
>
> If the next request goes to 5107,
> $SESSION_CLEANUP_COUNTER will start
> off with an "undef" value. If you keep quickly
> hitting "refresh" on the
> page, you should eventually see some
> incrementing--but it will be uneven
> because each apache2 process will have a different
> $SESSION_CLEANUP_COUNTER, and the assigning of HTTP
> requests to apache
> sub-processes seems to be semi-random.
>
> Mod_perl is great for making static data and module
> procedures more
> efficient; however, I think that data that *must* be
> updated in a linear
> fashion yet shared between several apache processes
> should be stored
> externally--in a database or a file (on ramdisk?) or
> in IPC.
>
> Again, this is my €0.0002 from my limited experience
> with mod_perl and a
> lot of deduction.
>
> Well perhaps there was a little induction too: try
> this program to prove
> to yourself that each mod_perl process has a
> different copy of the $sc
> variable:
>
> use strict;
> use warnings;
> our $sc;
> $sc++;
>
> print "Content-Type: text/html\n";
> print "\n";
>
> print qq{
> <title> Session Cleanup Test </title>
>
> <p> This is a test: $sc . <br>
> PID: $$
> </p>
>
> };
>
> __END__
>
> Keep hitting refresh. So long as the PID stays the
> same, $sc increments
> normally, but when apache assigns a new sub-process
> to deal with
> requests, $sc restarts at the beginning (1).
>
>
> --
> To unsubscribe, e-mail:
> beginners-unsubscribe@perl.org
> For additional commands, e-mail:
> beginners-help@perl.org
> http://learn.perl.org/
>
>
>





________________________________________
________________________________________
____
It's here! Your new message!
Get new email alerts with the free Yahoo! Toolbar.
http://tools.search.yahoo.com/toolbar/features/mail/
Mumia W.

2007-01-22, 6:58 pm

On 01/22/2007 04:17 PM, tom tom wrote:
>
> Can I know how to pass parameters from the CRON job
> e.g
> cleanup folder and delta time in seconds and
> thereafter retreive that from the perl.
>
> My cleanup rountine currently hardcoded with these
> values
> [...]


Why not just make it a regular, non-CGI, Perl script?

If it doesn't have to execute as a webserver CGI process, it probably
shouldn't.


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com