Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Many children, but never more than a fixed number at once.

by Abigail-II (Bishop)
on Jun 19, 2002 at 15:01 UTC ( #175715=snippet: print w/replies, xml ) Need Help??
Description: Some times you have a need to fork of several children, but you want to limit the maximum number of children that are alive at one time. Here are two little subroutines that might help you, mfork and afork. They are very similar. They take three arguments, and differ in the first argument. For mfork, the first argument is a number, indicating how many children should be forked. For afork, the first argument is an array - a child will be forked for each array element. The second argument indicates the maximum number of children that may be alive at one time. The third argument is a code reference; this is the code that will be executed by the child. One argument will be given to this code fragment; for mfork it will be an increasing number, starting at one. Each next child gets the next number. For afork, the array element is passed.

Note that this code will assume no other children will be spawned, and that $SIG {CHLD} hasn't been set to IGNORE

sub mfork ($$&) {
    my ($count, $max, $code) = @_;

    foreach my $c (1 .. $count) {
        wait unless $c <= $max;
        die "Fork failed: $!\n" unless defined (my $pid = fork);
        exit $code -> ($c) unless $pid;

    1 until -1 == wait;

sub afork (\@$&) {
    my ($data, $max, $code) = @_;
    my $c = 0;
    foreach my $data (@$data) {
        wait unless ++ $c <= $max;
        die "Fork failed: $!\n" unless defined (my $pid = fork);
        exit $code -> ($data) unless $pid;

    1 until -1 == wait;

Replies are listed 'Best First'.
Re: Many children, but never more than a fixed number at once.
by gav^ (Curate) on Jun 20, 2002 at 03:05 UTC
    I find Parallel::ForkManager good for this kind of thing as it lets you write:
    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 }
    Code stolen from docs :)


      Yeah, but that requires you to do three lines of code - to create an object (why? What's so OO about this?), and two calls (to ->start and ->finish) for each piece of code you want to execute.

      With mfork or afork, all you have is one call.

      But this is Perl, so there are more ways to do things.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://175715]
[shmem]: i.e accessed as $array[$i] where $i would be the loop index in a C-style "for" loop: for($i=0;$i<=$# array;$i++){ ... }</c>
[james28909]: i gotcha
[Discipulus]: reference is a road sign; alias like Constantinple and Istnbul
Discipulus well istanbul was a road sign but this is another story: eis ten polin
[shmem]: Discipulus++; # and I give you an "a"
[james28909]: and you are right, i already tried to go C but ended up coming back, mainly because data types confuse me, well that and things like long long double and float point things all scare me
[james28909]: lol
[Discipulus]: thanks for the "a" shemem was what I needed to get IstAnbul.. InstantBull
Discipulus has some problem with vowels.. sry

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2018-05-20 16:00 GMT
Find Nodes?
    Voting Booth?