Coro::ProcessPool - an asynchronous process pool
use Coro::ProcessPool; my $pool = Coro::ProcessPool->new( max_procs => 4, max_reqs => 100, ); my %result; foreach my $i (1 .. 1000) { $result{$i} = $pool->process(sub { shift * 2 }, $i); } $pool->shutdown;
Processes tasks using a pool of external Perl processes.
Creates a new process pool. Processes will be spawned as needed.
This is the maximum number of child processes to maintain. If all processes are busy handling tasks, further calls to "process" in . will yield until a process becomes available.
If this is a positive number (defaults to 0), child processes will be terminated and replaced after handling max_reqs tasks. Choosing the correct value for max_reqs is a tradeoff between the need to clear memory leaks in the child process and the time it takes to spawn a new process and import any packages used by client code.
max_reqs
Processes code ref $f in a child process from the pool. If $args is provided, it is an array ref of arguments that will be passed to $f. Returns the result of calling $f-(@$args)>.
$f
$args
$f-
This call will yield until the results become available. If all processes are busy, this method will block until one becomes available. Processes are spawned as needed, up to max_procs, from this method. Also note that the use of max_reqs can cause this method to yield while a new process is spawned.
max_procs
Shuts down all processes and resets state on the process pool. After calling this method, the pool is effectively in a new state and may be used normally.
Coro::ProcessPool will likely break on Win32 due to missing support for non-blocking file descriptors (Win32 can only call select and poll on actual network sockets). Without rewriting this as a network server, which would impact performance and be really annoying, it is likely this module will not support Win32 in the near future.
Coro::ProcessPool
select
poll
The following modules will get you started if you wish to explore a synchronous process pool on Windows:
Jeff Ober mailto:jeffober@gmail.com
BSD License
To install Coro::ProcessPool, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Coro::ProcessPool
CPAN shell
perl -MCPAN -e shell install Coro::ProcessPool
For more information on module installation, please visit the detailed CPAN module installation guide.