in reply to Problem with [mod://Fork::Manager]

The help for finish in Parallel::ForkManager say

Closes the child process by exiting

so therefore the child can't do any more work after the exit.

If you want to do work in the parent after starting a child then you'll need to do something like

while (@list) { if ($pm->start) { # do parent stuff next; } # child stuff goes here $pm->finish; }

BUT that seems a strange way to use Parallel::ForKManager, so maybe you need to rethink your design.

If you explain what you're really trying to do, perhaps someone here can make some useful suggestions.

Replies are listed 'Best First'.
Re^2: Help with multiple forks
by Anonymous Monk on May 30, 2012 at 12:41 UTC

    Thanks, I didn't get the crucial meaning about exiting in the documentation. Learning to understand the documentation is also learning - I remember how I struggled with man pages years ago :-/

    Here's what I want to do in pseudo-kind-of-code, apart from a bunch of extra loops that shouldn't have any influence.

    my @array1 = (1..5); # N my @array2 = qw/a b/; for my $val1 (@array1) { # Start N processes here that can run in parallel # Each process outputs data to its own separate file # I will need it in the future for my $val2 (@array2) { # For each of the N processes, wait until it is done, # then start 2 parallel processes which use # the output data as input # Save the output of each process separately to 2N files # (two files with N elements would be better, but as I # couldn't figure that out, I just postprocess the data ;-) } } # "waitallchildren" or equivalent # Postprocess step to reduce the final output to 2 files

    I just tried this, but it doesn't work. The outer part does, but as soon as I uncomment the inner part, my prompt doesn't return anymore. No idea what's going on.

    use Proc::Fork; for my $val1 (@array1) { run_fork { child { open FILE, ">$val1.txt"; print FILE "Output of step 1\n"; close FILE; } parent { my $child_pid_outer = shift; waitpid $child_pid_outer, 0; # for my $val2 (@array2) { # run_fork { # child { # open FILE1, "$val1.txt"; # open FILE2, ">$val2$val1.txt"; # while (my $line = <FILE1>) { # $line =~ s/1/2/; # print FILE2 $line . "\n"; # } # close FILE1; # close FILE2; # } # parent { # my $child_pid_inner = shift; # waitpid $child_pid_inner, 0; # } # Parent inner loop # }; # Inner fork # } # For loop } # Parent outer fork }; # Outer fork }

    I did have a (short) look at other packages, but I must admit I didn't understand much, unfortunately.

      I just wanted to mention that I reposted this under a more appropriate title, since I don't care about the module, as long as it works: Help with multiple forks. I got several answers through which I am now going through.