Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.

Problem: Everything I can find on google shows passing variables (e.g. scalars) to the thread queue rather than the subroutine itself... I think I'm just not understanding how to implement a thread queue properly compared to how you implement a thread (without queues).

Question 1: How can I "maintain a pool of threads" to avoid the perl interpreter from slowly eating up more and more memory?
Question 2: (Unrelated but while I have this code posted) Is there a safe amount of sleep (or a better method than sleep) at the end of the main program so that I don't start a thread more than once in a minute? 60 seems obvious but could that ever cause it to run more than once if the loop is fast, or perhaps miss a minute because of processing time or something?

Thanks in advance!



#!/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__

In reply to How do I queue perl subroutines to a thread queue instead of data? by static0verdrive

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-03-29 07:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found