Preceptor has asked for the wisdom of the Perl Monks concerning the following question:
I've been doing more and more threading recently - simple, but implicitly parallel tasks, like bulk statistics collection and graphing on servers.
Whilst I initially did a 'one thread per server' type of approach, I'm finding that doesn't scale as well as I'd like - a few hundred is not a problem, but a few thousand and it just doesn't work.
Thankfully, Thread::Queue and a set of 'worker' threads works quite nicely. Queue your 'server list', spawn N threads, and have them process the queue. Thus I tend to use something like:
sub worker_thread { while ( my $item = $processing_q -> dequeue() ) { # do stuff to $item } } for ( my $count = 0; $count < $nthreads; $count++ ) { my $thr = threads -> create ( \&worker_thread ); } $processing_q -> enqueue ( @server_list ); $processing_q -> end; foreach my $thread ( threads -> list() ) { $thread -> join(); }
This works quite nicely, but recently I've found I've needed to do two different 'sets' of worker threads - one to 'go and collect', and another set to process (and maybe some 'helper' threads, such as one to serialise data/logging output). I've been using queues again, and having the 'workers' feed the queue. However this is where I'm starting to have a bit more difficulty.
Does anyone have a neat idiom for 'handling' sub threads?
Currently, I've tried a variety of approaches:
my $thread_exclusion = Thread::Semaphore -> new ( -1 * $nthreads + 1 + );
In the 'worker' threads:
$thread_exclusion -> up();
And a 'helper' thread that all it does is close queues.
sub close_queues { $exclusion_lock -> down(); $process_q -> end(); }
This will block until the exclusion lock goes positive, e.g. all the 'worker' threads have finished.
$SIG{'ALRM'} = \&close_queues(); alarm ( 60 );
This has the advantage of being able to 'reset' the alarm - only one 'alarm' can be pending, so you can perform an 'alarm(60)' in each loop iteration to reset the clock.
I was wondering though - does anyone have a more elegant solution? I like how neat the 'while' loop is for queue processing.
|
|---|