NAME
Parallel::ForkManager - A simple parallel processing fork manager
SYNOPSIS
use Parallel::ForkManager;
$pm = new Parallel::ForkManager($MAX_PROCESSES);
foreach $data (@all_data) {
# Forks and returns the pid for the child:
my $pid = $pm->start and next;
... do some work with $data in the child process ...
$pm->finish; # Terminates the child process
}
DESCRIPTION
This module is intended for use in operations that can be done in parallel where the number of processes to be forked off should be limited. Typical use is a downloader which will be retrieving hundreds/thousands of files.
The code for a downloader would look something like this:
use LWP::Simple;
use Parallel::ForkManager;
...
@links=(
["http://www.foo.bar/rulez.data","rulez_data.txt"],
["http://new.host/more_data.doc","more_data.doc"],
...
);
...
# Max 30 processes for parallel download
my $pm = new Parallel::ForkManager(30);
foreach my $linkarray (@links) {
$pm->start and next; # do the fork
my ($link,$fn) = @$linkarray;
warn "Cannot get $fn from $link"
if getstore($link,$fn) != RC_OK;
$pm->finish; # do the exit in the child process
}
$pm->wait_all_childs;
First you need to instantiate the ForkManager with the "new" constructor. You must specify the maximum number of processes to be created. If you specify 0, then NO fork will be done; this is good for debugging purposes.
Next, use $pm->start to do the fork. $pm returns 0 for the child process, and child pid for the parent process (see also "fork()" in perlfunc(1p)). The "and next" skips the internal loop in the parent process. NOTE: $pm->start dies if the fork fails.
$pm->finish terminates the child process (assuming a fork was done in the "start").
NOTE: You cannot use $pm->start if you are already in the child process. If you want to manage another set of subprocesses in the child process, you must instantiate another Parallel::ForkManager object!
METHODS
- new $processes
-
Instantiate a new Parallel::ForkManager object. You must specify the maximum number of children to fork off. If you specify 0 (zero), then no children will be forked. This is intended for debugging purposes.
- start
-
This method does the fork. It returns the pid of the child process for the parent, and 0 for the child process. If the $processes parameter for the constructor is 0 then, assuming you're in the child process, $pm->start simply returns 0.
- finish
-
Closes the child process by exiting. If you use the program in debug mode ($processes == 0), this method doesn't do anything.
- wait_all_childs
-
You can call this method to wait for all the processes which has been forked. This is a blocking wait.
EXPERIMENTAL FEATURES
There are callbacks in the code, which can be called on events like starting a process or on finish. This code is not tested at all, hence the lack of documentation. If you want to try these features, please look at the code and test them. Feel free to send me patches if you find something wrong.
COPYRIGHT
Copyright (c) 2000 Szabó, Balázs (dLux)
All right reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
AUTHOR
dLux (Szabó, Balázs) <dlux@kapu.hu>
Noah Robin <sitz@onastick.net> (documentation tweaks)
2 POD Errors
The following errors were encountered while parsing the POD:
- Around line 81:
You forgot a '=back' before '=head1'
- Around line 87:
Non-ASCII character seen before =encoding in 'Szabó,'. Assuming CP1252