NAME
Proc::Forkmap - map with forking
SYNOPSIS
use Proc::Forkmap;
# basic usage
my @results = forkmap { process_item($_) } @items;
# with callback
forkmap { process_item($_) } @items, sub {
my ($idx, $result) = @_;
print "$result\n" if defined $result;
};
# with timeout
forkmap_settings(TIMEOUT => 50); # 50 second timeout
my @results = forkmap { long_running_task($_) } @items;
# with no IPC
forkmap_settings(IPC => 0); # no tempfiles created
forkmap { job($_) } @items;
DESCRIPTION
This module provides forkmap
, with syntax similar to Perl's built-in map
function. The function evaluates code blocks concurrently, with an optional callback.
forkmap
forkmap BLOCK LIST [, CODE reference]
forkmap_settings
# change your FM settings
forkmap_settings(
TIMEOUT => $seconds, # forked proc timeout in seconds (default: 0 = no timeout)
MAX_PROCS => $n, # maximum concurrent forked procs (default: 4)
IPC => 0, # enable return data via tempfiles (default: 1)
TEMPFILE_DIR => $dir # directory to store tempfiles (default: '/tmp')
);
Modify runtime parameters for forkmap
.
MAX_PROCS
Maximum number of concurrent forked processes (default: 4).
TIMEOUT
Time in seconds to allow each process to run before killed (default: 0 = no timeout).
IPC
Enable return data (default: 1).
If enabled, the return value of the BLOCK must be a scalar. The value is then sent via temporary files. If you need to return complex structures (arrays, hashes, objects), serialize them yourself inside the code block (for example using JSON or Storable).
TEMPFILE_DIR
Directory to store tempfiles (default: '/tmp').
CALLBACK MODE
If the last argument to forkmap
is a CODE reference, forkmap
invokes the callback. If the forked process completes, the callback is invoked either when the process completes (when IPC = 1) or before the process exits (when IPC = 0), for each process. The callback is called with ($index, $output)
, where $index
is the index of the item in the array mapped by the function, and $output
is the result returned by the code block for that item, if any. The result will be undefined unless IPC is enabled.
AUTHOR
Andrew Shapiro, <trski@cpan.org>
BUGS
Please report any bugs or feature requests to bug-proc-forkmap at rt.cpan.org
, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Proc-Forkmap. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
LICENSE AND COPYRIGHT
Copyright 2025 Andrew Shapiro.
This library is free software and may be distributed under the same terms as perl itself. See https://dev.perl.org/licenses/.