Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Threading Woes

by crackotter (Beadle)
on Feb 03, 2003 at 19:19 UTC ( [id://232364]=perlquestion: print w/replies, xml ) Need Help??

crackotter has asked for the wisdom of the Perl Monks concerning the following question:

I am writing a script to SNMP read and writes to DHCP hosts (using the Net::SNMP module). But unfortunitly I have 5000 hots at minumum for one past of the script. The values are in a hash which I use a 'foreach' to step through. I want to creat a queue of 20 threads. Do I used advanced semphores? And how do I make sure that no more than 20 threads are being created (none of the hash elements are being skipped)? Code samples would be helpful thanks, -Otter

Replies are listed 'Best First'.
Re: Threading Woes
by jasonk (Parson) on Feb 03, 2003 at 19:59 UTC
    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{$_}); } } }
      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
Re: Threading Woes
by perrin (Chancellor) on Feb 03, 2003 at 19:59 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://232364]
Approved by bart
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (1)
As of 2024-04-18 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found