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