Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Controlling Thread Numbers

by NetWallah (Canon)
on Oct 13, 2007 at 01:21 UTC ( #644579=note: print w/replies, xml ) Need Help??


in reply to Controlling Thread Numbers

Rather than starting/stopping threads to feed input, it would be preferable to feed them work to do. Here is a snipped of the code I use (In production on Windows/perl 58/ Activestate):
use threads; use Thread::Queue; our $KIDS = $count > 20? 10 : int ($count / 4) || 1; # Some way to c +ontrol max # of threads my $work_Q = new Thread::Queue; ## Start the kids my @kids = map{ threads->create( \&kid, $_) } 1 .. $KIDS; $work_Q->enqueue( keys %ServerInfo ); # Feed the QUEUE with work to do + .... ## Tell them to stop $work_Q->enqueue( (undef) x $KIDS ); # THis tells each thread to QUIT ## And wait for Kids to finish.. my %results = map {$_->join} @kids; #---------------------------------------------------- sub kid { my $kidnumber = shift; my @results=(); my $tid = threads->tid; my $count=0; my $pinger = Net::Ping->new("icmp",$TimeOut); #printf "Kid: %02d started<br>\n", $tid; while( my $work = $work_Q->dequeue ) { $count ++; push @results, $work, Ping_it($pinger, $work) ; } push @results,"KID$kidnumber",$count; #print "kid: $tid ending after processing $count items.<br>\n"; return @results; }

     "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom

Replies are listed 'Best First'.
Re^2: Controlling Thread Numbers
by BBQ (Deacon) on Oct 19, 2007 at 02:01 UTC
    Hey NetWallah,

    Thanks for the snippet!

    I'm having a hard time following though... on this line:

    our $KIDS   = $count > 20? 10 : int ($count / 4) || 1;

    Won't $KIDS always be assigned the value 1? Unless $count has already been assigned a value elsewhere... and I'm assuming that the keys in %ServerInfo are IP addresses that get passed to another sub_routine called Ping_it()?

    Sorry if I'm being a little dense here. Just trying to get what goes where and there seem to be a few pieces of the puzzle still missing.

    Thanks again!
      Sorry - the snippet was indeed incomplete without an explanation of external variables.

      "$count" comes from the number of servers I was processing - Under test conditions, it would be well under 20, and I would do all work in less than 5 sub processes/threads (Only one thread, if $count was less than 5). Under "Normal" conditions, it was in the 200 range, resulting in 10 threads.

      The %serverinfo was a HOH had other info besides IP - Server Name, and related info. I was laid off work on Tuesday 10/16, and no longer have access to the code I wrote, or I would gladly share it.

           "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2023-01-27 17:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?