static0verdrive has asked for the wisdom of the Perl Monks concerning the following question:
Background: In reading how to multithread my perl script, I read (from http://perldoc.perl.org/threads.html#BUGS-AND-LIMITATIONS)
On most systems, frequent and continual creation and destruction of threads can lead to ever-increasing growth in the memory footprint of the Perl interpreter. While it is simple to just launch threads and then ->join() or ->detach() them, for long-lived applications, it is better to maintain a pool of threads, and to reuse them for the work needed, using queues to notify threads of pending work.My script will be long-lived; it's an PKI LDAP directory monitoring daemon that will always be running. The enterprise monitoring solution will generate an alarm if it stops running for any reason. My script will check that I can reach another PKI LDAP directory, as well as validate revocation lists on both.
#!/usr/bin/perl use feature ":5.10"; use warnings; use strict; use threads; use Proc::Daemon; ### Global Variables use constant false => 0; use constant true => 1; my $app = $0; my $continue = true; $SIG{TERM} = sub { $continue = false }; # Directory Server Agent (DSA) info my @ListOfDSAs = ( { name => "Myself (inbound)", host => "ldap.myco.ca", base => "ou=mydir,o=myco,c=ca", }, { name => "Company 2", host => "ldap.comp2.ca", base => "ou=their-dir,o=comp2,c=ca", } ); ### Subroutines sub checkConnections { # runs every 5 minutes my (@DSAs, $logfile) = @_; # Code to ldapsearch threads->detach(); } sub validateRevocationLists { # runs every hour on minue xx:55 my (@DSAs, $logfile) = @_; # Code to validate CRLs haven't expired, etc threads->detach(); } ### Main program Proc::Daemon::Init; while ($continue) { my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localti +me(time); # Question 1: Queues?? if ($min % 5 == 0 || $min == 0) { threads->create(&checkConnections, @ListOfDSAs, "/var/connec +t.log"); } if ($min % 55 == 0) { threads->create(&validateRevocationLists, @ListOfDSAs, "/var +/RLs.log"); } sleep 60; # Question 2: Safer/better way to prevent multiple threa +ds being started for same check in one matching minute? } # TERM RECEIVED exit 0; __END__
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How do I queue perl subroutines to a thread queue instead of data?
by Random_Walk (Prior) on Apr 25, 2013 at 14:03 UTC | |
by static0verdrive (Initiate) on Apr 25, 2013 at 14:43 UTC | |
by Random_Walk (Prior) on Apr 25, 2013 at 15:06 UTC | |
by static0verdrive (Initiate) on Apr 25, 2013 at 15:14 UTC | |
|
Re: How do I queue perl subroutines to a thread queue instead of data?
by locked_user sundialsvc4 (Abbot) on Apr 26, 2013 at 01:46 UTC |