Sounds like you need the Thread::Pool module, it will setup a pool of threads for you.
use Thread::Pool;
$pool = Thread::Pool->new(
{
do => sub {shift; print "do(@_)\n"; return $result; },
workers => 20,
},
);
# then you submit your jobs
foreach(keys %jobs) {
$ids{$_} = $pool->job(...arguments to do...);
}
while(keys %ids) {
foreach(keys %ids) {
if(@result = $pool->result_dontwait($_)) {
$results{$_} = [@result];
delete($ids{$_});
}
}
}
| [reply] [d/l] |
Hi, I tried what you said about Thread::Pool, and all the threads get started correctly but once I get to the 'if statement' that kills the threads, the foreach loop, get stuck in a infinte loops. I pasted some of my code in here, with a dummy sub function I created to test the pools. Any idea why the 'if(@result = $pool->result_dontwait($_))' statement never hits true??
-Otter
my %hosts=();
my $counter=0;
my $global=0;
my $pool=();
my @result=();
my %ids=();
$pool = Thread::Pool->new(
{
do => sub {\&Temp_function(@_);},
workers => 10, # default: 1
});
%hosts = &Telnet_host();
foreach (keys %hosts)
{
$ids{$_}=$pool->job($_, $hosts{$_} );
}
while (keys %ids)
{
foreach (keys %ids)
{
if (@result = $pool->result_dontwait($_))
{
print $ids{$_} . "\n";
delete ($ids{$_});
}
}
print "Items still to do: " . $pool->todo . "\n";
}
##--END FUNCTION CALLs--
### Temp Sub Functions
sub Temp_function
{
my ($ip,$mac)= @_;
print $mac . "\n";
return $mac;
}
### End Temp Sub Functions
| [reply] [d/l] |