in reply to Re^2: missing values when using Parallel::ForkManager
in thread missing values when using Parallel::ForkManager

I wrote a mini code for this problem which replicates my puzzle.
#!/usr/bin/perl use strict; use Parallel::ForkManager; my $pm = Parallel::ForkManager->new(10); foreach my $j (1..100) { LINKS: for my $i (1..10) { $pm->start and next LINKS; my $num = count($i); print $num,"\t"; $pm->finish; } print "\n"; $pm->wait_all_children; } sub count { my $num = shift; return $num + 1; }
I expected each row should generate ten numbers from 2 to 11. However, several rows will miss values. Even worse, several rows output 11 numbers instead of 10 numbers, and have replicate, for example, 10 appear twice. Why?

Replies are listed 'Best First'.
Re^4: missing values when using Parallel::ForkManager (order)
by tye (Sage) on Jul 07, 2015 at 04:07 UTC

    I don't get any missed numbers; I always get 2..11. I moved the wait_all_children() line above the line that prints a newline so that the output is clearer and more consistent:

    2 3 4 5 6 7 9 8 10 + 11 2 3 4 5 6 7 8 9 10 + 11 2 3 4 5 6 7 8 9 10 + 11 2 3 4 5 6 7 8 9 10 + 11 2 3 4 5 6 7 8 9 10 + 11 2 3 4 5 6 7 8 9 10 + 11 2 3 4 5 6 7 8 9 10 + 11 2 3 4 5 6 7 8 9 10 + 11 2 3 4 5 6 7 8 9 10 + 11 2 3 4 5 6 7 8 9 10 + 11 2 3 4 5 6 7 10 9 8 + 11 2 3 4 5 6 7 9 10 11 + 8 2 3 4 5 6 7 9 8 11 + 10 2 3 4 5 6 7 8 9 10 + 11 2 3 4 5 6 7 9 11 8 + 10 2 3 4 5 6 7 8 9 10 + 11 2 3 4 5 6 7 8 9 10 + 11 2 3 4 5 6 7 8 9 10 + 11 2 3 4 5 6 8 9 7 11 + 10 2 3 4 5 6 7 9 10 8 + 11 2 3 4 5 6 7 8 9 10 + 11 2 3 4 5 6 7 8 9 10 + 11 2 3 4 5 6 7 8 9 10 + 11 2 3 4 5 6 7 8 9 10 + 11 2 3 4 5 6 7 8 9 10 + 11 2 3 4 5 6 8 9 7 11 + 10 2 3 4 5 6 7 8 9 10 + 11 2 3 4 5 6 7 8 9 10 + 11 2 3 4 5 6 7 8 9 10 + 11 2 3 4 5 6 7 8 9 10 + 11 2 3 4 5 6 8 9 10 7 + 11 2 3 4 5 6 7 8 10 9 + 11 2 3 4 5 6 7 9 10 8 + 11 2 3 4 5 6 7 8 9 11 + 10 2 3 4 5 6 7 9 10 8 + 11 2 3 4 5 6 7 8 9 10 + 11 2 3 4 5 6 7 8 9 10 + 11 2 3 4 5 6 7 9 10 8 + 11

    Doing things in parallel means that you can't guarantee in what order they will finish. Sometimes one process takes a bit longer than the others and so the numbers appear "out of order".

    - tye        

Re^4: missing values when using Parallel::ForkManager
by Anonymous Monk on Jul 07, 2015 at 02:46 UTC
      You and Tye reminds me of the right solution to my problem.It seems that the FileHandle was not updated in child process. I therefore created a FileHandle in each child process instead of the parent process. Anyway, it is my own understanding. If someone know it, do drop me your message.