NAME
Fork::Queue - Perl extension to limit the number of concurrent child process running
SYNOPSIS
use Fork::Queue size => 4, debug => 1;
package other;
# this loop will create new childs, but Fork::Queue will make it
# wait when the limit (4) is reached until some of the old childs
# exit.
foreach (1..10) {
my $f=fork;
if(defined ($f) and $f==0) {
print "-- I'm a forked process $$\n";
sleep rand 5;
print "-- I'm tired, going away $$\n";
exit(0)
}
}
Fork::Queue::size(10); # changing limit to 10 concurrent processes
Fork::Queue::trace(1); # trace mode on
Fork::Queue::debug(0); # debug is off
package other; # just to test it works in any package
print "going again!\n";
# another loop with different settings for Fork::Queue
foreach (1..20) {
my $f=fork;
if(defined ($f) and $f==0) {
print "-- I'm a forked process $$\n";
sleep rand 5;
print "-- I'm tired, going away $$\n";
exit(0)
}
}
1 while wait != -1;
DESCRIPTION
This module lets you parallelice one program using the fork, exit, wait and waitpid calls as usual and without the need to take care of creating too much processes and overloading the machine.
It works redefining fork, exit, wait and waitpid functions so old programs do not have to be modified to use this module (only the 'use Fork::Queue' sentence is needed).
Additionally, the module have two debugging modes (debug and trace) that can be activated and that seem too be very useful when developing parallel aplications.
Debug mode when activated dumps lots of information about processes being created, exiting, being caught be parent, etc.
Trace mode just prints a line every time one of the fork, exit, wait or waitpid functions is called.
Childs processes continue to use the modified functions, but its queues are reset and the maximun process number for them is set to 1. Althought child can change it to any other value if needed.
EXPORT
This module redefines the fork, wait and exit calls
FUNCTIONS
There are several not exported functions that can be used to configure the module:
- size(), size(int)
-
If an argument is given the maximun number of concurrent processes is set to it and the number of maximun processes that were allowed before is returned.
If no argument is given, the number of processes allowed is returned.
- debug(), debug(boolean), trace(), trace(boolean)
-
Change or return the status for the debug and trace modes.
- import(name,%options)
-
The import functions is not usually explicitally called but by the
use Fork::Queue
statement. The options allowed aresize
,debug
andtrace
and they let you configure the module instead of using thesize
,debug
ortrace
module functions as in...use Fork::Queue size=>10, debug=>1;
BUGS
None that I know, but this is just version 0.01!
Child behaviuor althought deterministic could be changed to something better. I would accept any suggestions on it.
AUTHOR
Salvador Fandino <sfandino@yahoo.com>
SEE ALSO
perlfunc(1), perlfork(1), Parallel::ForkManager.
1 POD Error
The following errors were encountered while parsing the POD:
- Around line 300:
You forgot a '=back' before '=head2'