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
|