Proc::tored::Pool - managed work pool with Proc::tored and Parallel::ForkManager
version 0.01
use Proc::tored::Pool; # Create a worker pool service my $pool = pool 'thing-doer', in '/var/run', capacity 10, on success, call { my ($me, $id, @results) = @_; print "thing $id complete: @results"; }, on failure, call { my ($me, $id, $message) = @_; warn "thing $id failed: $message"; }; # Do things with the pool run { my ($thing_id, $thing) = get_next_thing(); process { do_things($thing) } $pool, $thing_id; } $pool; # Control the pool as a Proc::tored service zap $pool, 15 or die "timed out after 15 seconds waiting for pool to stop";
Provides a simple and fast interfact to build and manage a pool of forked worker processes. The process is controlled using a pidfile and POSIX signals.
As a Proc::tored::Pool is a Proc::tored service, it by default exports the same functions as "EXPORTED SUBROUTINES" in Proc::tored.
Proc::tored::Pool
In addition, the following subroutines are exported by default.
Creates the pool (an instance of Proc::tored::Pool::Manager). Requires a $name as its first argument.
$name
my $pool = pool 'the-proletariat', ...;
Sets the max number of forked worker processes to be permitted at any given time.
my $pool = pool 'the-proletariat', capacity 16, ...;
Builds an event callback with one of "assignment", "success", or "failure".
my $pool = pool 'the-proletariat', capacity 16, on success, call { ... };
Defines the code to be called by an event callback. See "on".
Returns the number of tasks that have been assigned to worker processes but have not yet completed.
Sends a task (a CODE ref) to the pool, optionally specifying a task id to identify the results in callbacks. The return value of the supplied code ref is passed as is to the "success" callback (if supplied).
CODE
process { seize_the_means_of_production() } $pool; process { seize_the_means_of_production() } $pool, $task_id;
For situations in which a task or tasks must be completed before program execution can continue, sync may be used to block until all pending tasks have completed. After calling sync, there will be no pending tasks and all callbacks for previously submitted tasks will have been called.
sync
process { seize_the_means_of_production() } $pool; sync $pool;
Triggered immediately after a task is assigned to a worker process. Receives the pool object and the task id (if provided when calling "pool").
my $pool = pool 'thing-doer', ..., on assignment, call { my ($self, $task_id) = @_; $assigned{$task_id} = 1; }; process { do_things() } $pool, $task_id;
Triggered after the completion of a task. Receives the pool object, task id (if provided when calling "pool"), and the return value of the code block.
my $pool = pool 'thing-doer', ..., on success, call { my ($self, $task_id, @result) = @_; ... }; process { do_things() } $pool, $task_id;
Triggered if the code block dies or the forked worker exits abnormally. Recieves the pool object, task id (if provided when calling "pool"), and the error message generated by the code ref.
my $pool = pool 'thing-doer', ..., on failure, call { my ($self, $task_id, $error) = @_; warn "Error executing task $task_id: $error"; }; process { do_things() } $pool, $task_id;
The same warnings and limitations pertain to Proc::tored::Pool as apply to Parallel::ForkManager, including an injunction against using two pools simultaneously from the same process. See "BUGS AND LIMITATIONS" in Parallel::ForkManager and "SECURITY" in Parallel::ForkManager for details.
Proc::tored, Parallel::ForkManager
Jeff Ober <jeffober@gmail.com>
This software is copyright (c) 2017 by Jeff Ober.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Proc::tored::Pool, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Proc::tored::Pool
CPAN shell
perl -MCPAN -e shell install Proc::tored::Pool
For more information on module installation, please visit the detailed CPAN module installation guide.