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????
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 | |
by ravi45722 (Pilgrim) on Nov 20, 2015 at 08:31 UTC | |
by Corion (Patriarch) on Nov 20, 2015 at 08:42 UTC | |
|