This sounds like a perfect application for threading, lots of IO wait states and (I assume) little or no interaction required between the threads.

I would also concur that a thread pool is the right mechanism, though that's not ane endorsement of Thread::Pool if in fact such a module exists, as I (obviously) have never tried it.

The type of model I would use for this goes something like the following (untested, pseudo-code).

#! perl -w use strict; require 5.008; use threads; use Thread::Queue; # Adjust (slowly) till you find the optimum use constant MAX_THREADS => 10; my $dispatchQ = Thread::Queue->new(); my $resultsQ = Thread::Queue->new(); sub worker { # While there's stuff to be done while( $dispatchQ->pending() ) { # Get the next serverb my $server = $dispatchQ->dequeue(); # creat a new connection (A guess!!) my $smtp = Net::SMTP->new( $server ); # do whatever is required my $result = 'Whatever'; # Tack the server name on the front of the results # for later identification. The normal perl idiom of # using an (anonymous) array has the problem that it # causes rapid memory leaks. Using this simplistic # approach seems to be most reliable and scalable. $resultsQ->enqueue( join( $; , $server, $result ) ); } return; # When there is nothing left to do, die. } # Create the pool my @threads = map{ threads->create( \&worker ) } 1 .. MAX_THREADS; # Read the list of servers my @servers = do{ local (*ARGV, $/) = 'servers.list'; <> }; # Put the list on the queue $dispatchQ->enqueue( @servers ); # While there is still work to be done while( $dispatchQ->pending() ) { # Get the results as they become available my ($server, $result) = split $;, $resultsQ->dequeue(); # Record the results somewhere. } # DispatchQ is empty, each thread will terminate once it completes its + last task $_->join for @threads; # Wait till they finish # Record the last few results while( my ($server, $result) = @{ $resultsQ->dequeue } ) { # Record the results somewhere. } __END__

This is extremely speculative code, but derived from some stuff I am playing with that seems to work quite well. I'd love some feedback (either way) if you use it.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller



In reply to Re: Thread Advice by BrowserUk
in thread Thread Advice by crackotter

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.