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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.