after each child finishes there should be another one created
That's exactly what Parallel::ForkManager does.
Say you have 5 tasks, and you set a max of 3 children. ForkManager will create 3 children and assign them tasks 0, 1 and 2. When one of these end, another child is created for task 3. When one of child ends, another child is created for task 4. Having no more tasks to assign, it waits for the 3 children to end.
The implementation is very simple:
use Parallel::ForkManager qw( ) use constant MAX_PROCESSES => 3; my $pm = Parallel::ForkManager->new(MAX_PROCESSES); foreach $data (@array) { # 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 }
In reply to Re^3: Forking problem (manager)
by ikegami
in thread Forking problem UPDATED
by avi_learns_perl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |