Parallel::QueueWorker - Simple worker support do jobs in parallel processes.
use Parallel::QueueWorker; my $worker = Parallel::QueueWorker->new( # config file will load configfile => "$FindBin::Bin/../etc/app.yml", # callback/handler # loop to prepare job queue prepare_queue => sub { my ($self) = @_; $self->add_job({_id => $_ }) for (0..99); $self->{done} = 1 unless $self->{done}; # if you return non-zero , the prepare_queue will loop next, unless ($self->{done}) { $self->{done} = 1; return 100; } # this will flag no jobs queued anymore and break prepare_queue loop. # but, not, this is invoke again! return 0; }, # before call work will run this. before_work = sub { my ($self); # If you want to open resource,like socket,dbi, should in here. }, # work code work => sub { my ($self,$job) = @_; $self->say("job id:",$job->{_id}); }, );
$worker->run;
Start to process jobs , it will wait until all queued jobs done.
# add job to queue $worker->add_job({_id => 3,foo => 'bar'});
Add job to the internal job queue. You should call this on prepare_queue callback.
Callback will invoke before call work.
work prototype => sub { my ($self,$job ) }
Your work code, this code will fork and parallel running to process job.
The code to feed jobs into queue. If return non-zero, this code will invoke again after work, until it return 0, so you can make the code loop run or just once.
A helpful function to output message handy, additional master or worker PID.
$self->say('hello'); # in master, output,xxxx meant master PID master xxxx >> hello # in worker, output,xxxx meant worker(childen process) PID worker xxxx >> hello
Internal, fork workers and process queued jobs until jobs all done.
Start to run, its will call prepare_queue code, if any job is queued, then fork workers to process them in parallel.
Night Sailer(Pan Fan) < nightsailer{at}gmail dot com >
copyright nightsailer.com
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Parallel::QueueWorker, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Parallel::QueueWorker
CPAN shell
perl -MCPAN -e shell install Parallel::QueueWorker
For more information on module installation, please visit the detailed CPAN module installation guide.