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

Hi All, I a new to perl scripting,need a help in implementing the below logic. I have scenerio where I need to call block i.e modules through for loop
for (i=0; i<8000 ;i++) { BLOCK1 { .. .. .. subroutine 1; subroutine 2; } } I want this to be run in parallel BLOCK1 process1 { ... ... subroutine 1; subroutine 2; } BLOCK1 process2 { .... .... subroutine 1; subroutine 2; } ... ... BLOCK1 process8000 { .... .... subroutine 1; subroutine 2; }
Is there way in perl to do this . Thanks, gvk I want the loop to looped around 7000 times and I need to wait to finish all the process before I proceed to next step

Replies are listed 'Best First'.
Re: PARALLEL PROCESSING IN PERL
by moritz (Cardinal) on Apr 14, 2012 at 06:32 UTC
Re: PARALLEL PROCESSING IN PERL
by zentara (Cardinal) on Apr 14, 2012 at 16:38 UTC
    If you want to do it without a module or threads, here is a set of subs from an honored old monk abigail. It's old style code ( ala Perl4) but still works well.
    #!/usr/bin/perl #by Abigail of perlmonks.org #Some times you have a need to fork of several children, but you want +to #limit the maximum number of children that are alive at one time. Here #are two little subroutines that might help you, mfork and afork. They + are very similar. #They take three arguments, #and differ in the first argument. For mfork, the first #argument is a number, indicating how many children should be forked. +For #afork, the first argument is an array - a child will be #forked for each array element. The second argument indicates the maxi +mum #number of children that may be alive at one time. The third argument +is a #code reference; this is the code that will be executed by the child. +One #argument will be given to this code fragment; for mfork it will be an + increasing number, #starting at one. Each next child gets the next number. For afork, the + array element is #passed. Note that this code will assume no other children will be spa +wned, #and that $SIG {CHLD} hasn't been set to IGNORE. mfork (10,10,\&hello); sub hello{print "hello world\n";} print "all done now\n"; ################################################### sub mfork ($$&) { my ($count, $max, $code) = @_; foreach my $c (1 .. $count) { wait unless $c <= $max; die "Fork failed: $!\n" unless defined (my $pid = fork); exit $code -> ($c) unless $pid; } 1 until -1 == wait; } ################################################## sub afork (\@$&) { my ($data, $max, $code) = @_; my $c = 0; foreach my $data (@$data) { wait unless ++ $c <= $max; die "Fork failed: $!\n" unless defined (my $pid = fork); exit $code -> ($data) unless $pid; } 1 until -1 == wait; } #####################################################

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh