threads::farm - group of threads for performing similar jobs
use threads::farm; $farm = threads::farm->new( { autoshutdown => 1, # default: 1 = yes workers => 5, # default: 1 pre => sub {shift; print "starting worker with @_\n", do => sub {shift; print "doing job for @_\n"; reverse @_}, post => sub {shift; print "stopping worker with @_\n", }, qw(a b c) # parameters to pre-job subroutine ); $farm->job( qw(d e f) ); # not interested in result $jobid = $farm->job( qw(g h i) ); @result = $farm->result( $jobid ); # wait for result to be ready print "Result is @result\n"; $jobid = $farm->job( qw(j k l) ); @result = $farm->result_nb( $jobid ); # do _not_ wait for result print "Result is @result\n"; # may be empty when not ready yet $farm->hire; # add worker(s) $farm->fire; # remove worker(s) $farm->workers( 10 ); # set number of workers $hired = $farm->hired; $fired = $farm->fired; print "$hired workers hired, $fired workers fired\n"; $todo = $farm->todo; $done = $farm->done; print "$done jobs done, still $todo jobs todo\n"; $farm->autoshutdown( 1 ); # shutdown when object is destroyed $farm->shutdown; # wait until all jobs done @pre = shift->pre; # inside do() and post() only;
Passing unshared values between threads is accomplished by serializing the specified values using Storable. This allows for great flexibility at the expense of more CPU usage. It also limits what can be passed, as e.g. code references can not be serialized and therefor not be passed.
Storable
threads, Thread::Queue::Any, Storable.
To install threads::farm, copy and paste the appropriate command in to your terminal.
cpanm
cpanm threads::farm
CPAN shell
perl -MCPAN -e shell install threads::farm
For more information on module installation, please visit the detailed CPAN module installation guide.