=head1 NAME AnyEvent::Fork::Pool - simple process pool manager on top of AnyEvent::Fork =head1 SYNOPSIS use AnyEvent; use AnyEvent::Fork::Pool; # use AnyEvent::Fork is not needed # all parameters with default values my $pool = AnyEvent::Fork ->new ->require ("MyWorker") ->AnyEvent::Fork::Pool::run ( "MyWorker::run", # the worker function # pool management max => 4, # absolute maximum # of processes idle => 2, # minimum # of idle processes load => 2, # queue at most this number of jobs per process start => 0.1, # wait this many seconds before starting a new process stop => 1, # wait this many seconds before stopping an idle process on_destroy => (my $finish = AE::cv), # called when object is destroyed # parameters passed to AnyEvent::Fork::RPC async => 0, on_error => sub { die "FATAL: $_[0]\n" }, on_event => sub { my @ev = @_ }, init => "MyWorker::init", serialiser => $AnyEvent::Fork::RPC::STRING_SERIALISER, ); for (1..10) { $pool->(doit => $_, sub { print "MyWorker::run returned @_\n"; }); } undef $pool; $finish->recv; =head1 DESCRIPTION This module uses processes created via L and the RPC protocol implement in L to create a load-balanced pool of processes that handles jobs. Understanding of L is helpful but not critical to be able to use this module, but a thorough understanding of L is, as it defines the actual API that needs to be implemented in the children. =head1 EXAMPLES =head1 PARENT USAGE =over 4 =cut package AnyEvent::Fork::Pool; use common::sense; use Scalar::Util (); use Guard (); use Array::Heap (); use AnyEvent; use AnyEvent::Fork; # we don't actually depend on it, this is for convenience use AnyEvent::Fork::RPC; # these are used for the first and last argument of events # in the hope of not colliding. yes, I don't like it either, # but didn't come up with an obviously better alternative. my $magic0 = ':t6Z@HK1N%Dx@_7?=~-7NQgWDdAs6a,jFN=wLO0*jD*1%P'; my $magic1 = '<~53rexz.U`!]X[A235^"fyEoiTF\T~oH1l/N6+Djep9b~bI9`\1x%B~vWO1q*'; our $VERSION = 0.1; =item my $rpc = AnyEvent::Fork::Pool::run $fork, $function, [key => value...] The traditional way to call it. But it is way cooler to call it in the following way: =item my $rpc = $fork->AnyEvent::Fork::Pool::run ($function, [key => value...]) Creates a new pool object with the specified C<$function> as function (name) to call for each request. The pool uses the C<$fork> object as the template when creating worker processes. You can supply your own template process, or tell C to create one. A relatively large number of key/value pairs can be specified to influence the behaviour. They are grouped into the categories "pool management", "template process" and "rpc parameters". =over 4 =item Pool Management The pool consists of a certain number of worker processes. These options decide how many of these processes exist and when they are started and stopp.ed =over 4 =item idle => $count (default: 0) The minimum amount of idle processes in the pool - when there are fewer than this many idle workers, C will try to start new ones, subject to C and C. This is also the initial/minimum amount of workers in the pool. The default of zero means that the pool starts empty and can shrink back to zero workers over time. =item max => $count (default: 4) The maximum number of processes in the pool, in addition to the template process. C will never create more than this number of worker processes, although there can be more temporarily when a worker is shut down and hasn't exited yet. =item load => $count (default: 2) The maximum number of concurrent jobs sent to a single worker process. Worker processes that handle this number of jobs already are called "busy". Jobs that cannot be sent to a worker immediately (because all workers are busy) will be queued until a worker is available. =item start => $seconds (default: 0.1) When a job is queued and all workers are busy, a timer is started. If the timer elapses and there are still jobs that cannot be queued to a worker, a new worker is started. This configurs the time that all workers must be busy before a new worker is started. Or, put differently, the minimum delay betwene starting new workers. The delay is zero by default, which means new workers will be started without delay. =item stop => $seconds (default: 1) When a worker has no jobs to execute it becomes idle. An idle worker that hasn't executed a job within this amount of time will be stopped, unless the other parameters say otherwise. =item on_destroy => $callback->() (default: none) When a pool object goes out of scope, it will still handle all outstanding jobs. After that, it will destroy all workers (and also the template process if it isn't referenced otherwise). =back =item Template Process The worker processes are all forked from a single template process. Ideally, all modules and all cdoe used by the worker, as well as any shared data structures should be loaded into the template process, to take advantage of data sharing via fork. You can create your own template process by creating a L object yourself and passing it as the C