Proc::Queue(3pm) | User Contributed Perl Documentation | Proc::Queue(3pm) |
Proc::Queue - limit the number of child processes running
use Proc::Queue size => 4, debug => 1; package other; use POSIX ":sys_wait_h"; # imports WNOHANG # this loop creates new children, but Proc::Queue makes it wait every # time the limit (4) is reached until enough children exit foreach (1..100) { 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 waitpid(-1, WNOHANG)>0; # reaps children } Proc::Queue::size(10); # changing limit to 10 concurrent processes Proc::Queue::trace(1); # trace mode on Proc::Queue::debug(0); # debug is off Proc::Queue::delay(0.2); # set 200 miliseconds as minimum # delay between fork calls package other; # just to test it works on any package print "going again!\n"; # another loop with different settings for Proc::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;
This module lets you parallelise a perl program using the "fork", "exit", "wait" and "waitpid" calls as usual but without taking care of creating too many processes and overloading the machine.
It redefines perl "fork", "exit", "wait" and "waitpid" core functions. Old programs do not need to be reprogrammed, only the "use Proc::Queue ..." sentence has to be added to them.
Additionally, the module has two debugging modes (debug and trace) that seem too be very useful when developing parallel aplications:
It is also possible to set a minimun delay time between fork calls to stop too many processes for starting in a short time interval.
Child processes continue to use the modified functions, but their queues are reset and the maximun process number for them is set to 1 (anyway, children can change their queue size themselves).
Proc::Queue doesn't work if CHLD signal handler is set to "IGNORE".
Internally, Proc::Queue, automatically catches zombies and stores their exit status in a private hash. To avoid leaking memory in long running programs you have to call "wait" or "waitpid" to delete entries from that hash or alternatively active the "ignore_children" mode:
Proc::Queue::ignore_children(1)
or
use Proc::Queue ignore_children=>1, ...
This module redefines the "fork", "wait", "waitpid" and "exit" calls.
Functions "fork_now", "waitpids", "run_back", "run_back_now", "all_exit_ok", "running_now", "system_back" and "system_back_now" can be imported. Tag ":all" is defined to import all of them.
There are several not exported functions that can be used to configure the module:
If no argument is given, the number of processes allowed is returned.
If Time::HiRes module is available delays shorted that 1 second are allowed.
If no arg is given, the current delay is returned.
To clear it use Proc::Queue::delay(0).
Proc::Queue::weight(3); run_back { ... heavy process here ... }; Proc::Queue::weight(1);
causes the "heavy process" to count as three normal processes.
Valid weight values are integers greater than zero.
Remember to reset the weight back to 1 (or whatever) after the heavier processes have been forked!.
Setting "allow_excess" to any value greater than zero (default is 1) resets the default behavior.
Proc::Queue::ignore_children(1);
is the equivalent to
$SIG{CHLD}='IGNORE'
when using Proc::Queue.
Other utility subroutines that can be imported from Proc::Queue are:
use Proc::Queue size => 5, qw(fork_now), debug =>1; $f=fork_now; if(defined $f and $f == 0) { print "I'm the child\n"; exit; }
Returns the pid of the forked process or undef if the program was not found.
Options allowed are "size", "debug", "weight" and "trace", i.e:
use Proc::Queue size=>10, debug=>1;
Anything that is not "size", "debug", "weight" or "trace" is expected to be a function name to be imported.
use Proc::Queue size=>10, ':all';
Proc::Queue is a very stable module, no bugs have been reported for a long time.
Support for Win32 OSs is still experimental.
perlfunc(1), perlipc(1), POSIX, perlfork(1), Time::HiRes, Parallel::ForkManager. The "example.pl" script contained in the module distribution.
Copyright 2001-2003, 2005-2008 by Salvador Fandiño <sfandino@yahoo.com>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
2021-01-07 | perl v5.32.0 |