ravi45722 has asked for the wisdom of the Perl Monks concerning the following question:

I want to try program on process. As monks suggested I am using Parallel::ForkManager. As per module document I tried this code.

#!/usr/bin/perl use warnings; use strict; use Benchmark; my $t0 = Benchmark->new; use Parallel::ForkManager; my @links=("ALPHA","Numeric"); # Max 30 processes for parallel download my $pm = Parallel::ForkManager->new(2); sub alpha { foreach my $i (900 .. 1900) { print 'A',"\t"; } } sub numeric { foreach my $j (1 .. 1000) { print $j,"\t"; } } LINKS: foreach my $linkarray (@links) { $pm->start and next LINKS; # do the fork if ($linkarray eq "ALPHA") { alpha(); } if ($linkarray eq "Numeric") { numeric(); } $pm->finish; # do the exit in the child process } $pm->wait_all_children; my $t1 = Benchmark->new; my $td = timediff($t1, $t0); print "the code took:",timestr($td),"\n";

But the output is not like what I expected. Its printing A thousand time and after 1..1000 and again A's 1000 times and again 1..1000. What I expected is if the process running parallel A's 50 (suppose) and then 1..50 and again A and again numeric's like this. I am doing anything wrong????

Update

Small minor change in code

Replies are listed 'Best First'.
Re: Odd behaviour of Parallel::ForkManager
by Corion (Patriarch) on Nov 20, 2015 at 07:45 UTC

    You have enabled use warnings;. The next step is to read the warnings that Perl outputs:

    C:\Users\Corion>perl -wc Projekte\tmp.pl Found = in conditional, should be == at Projekte\tmp.pl line 37. Found = in conditional, should be == at Projekte\tmp.pl line 41. Projekte\tmp.pl syntax OK

    What have you done to address these warnings?

      I replaced "=" with "eq". Now the warning is not there.

        If you expect interleaved output, you will need to disable output buffering and make your code much slower:

        sub alpha { foreach my $i (900 .. 1900) { print 'A',"\t"; sleep rand(2); } } sub numeric { foreach my $j (1 .. 1000) { print $j,"\t"; sleep rand(2); } } # Make print() output unbuffered; $| = 1; LINKS: foreach my $linkarray (@links) { $pm->start and next LINKS; # do the fork ...
        A reply falls below the community's threshold of quality. You may see it by logging in.