Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
"be consistent"
 
PerlMonks  

Re: Parallel::ForkManager How to wait when 1st 10 Child processe are over out of 100

by Tanktalus (Canon)
on Dec 09, 2011 at 00:27 UTC ( #942524=note: print w/ replies, xml ) Need Help??


in reply to Parallel::ForkManager How to wait when 1st 10 Child processe are over out of 100

I know you're using P::FM. However, I have to wonder if this would be easier to do with AnyEvent. I'm not entirely sure, as after about 10 minutes, I still don't have example code quite working :-) but maybe it's something to start with.

use 5.14.0; use EV; use AnyEvent; use AnyEvent::Util qw(run_cmd); use File::Spec; # set up my condvars my $all_done = AE::cv; $all_done->begin for 1..50; my $ten = AE::cv; # start the first 10... start_ten($ten, $all_done, $_) for 1..2; # wait for all of them to finish. $all_done->recv(); sub start_ten { my $t = shift; my $all = shift; my $one = shift; state $started = 0; # 10/100 too much for my machine... for (1..5) { $started ++; my $proc = $started; $t->begin if $one == 1; $all->end; print "Starting...$proc\n"; my $cv = run_cmd( ['sleep', rand(3)], '>' => File::Spec->devnull(), '2>' => File::Spec->devnull(), '<' => File::Spec->devnull(), ); $cv->cb( sub { my $c = shift; my $rc = $c->recv(); print "Finished $proc: $rc\n"; $t->end() }); } # when we're done, start the next ten. $t->cb( sub { my $c = shift; print "Checking next group...($started)\n"; $c->recv; $all->ready() or start_ten($t, $all, 1) }); }
There is some tweaking required - this will start the first two groups of five, and wait - once the first five come in, it goes and starts the rest. It's not waiting properly. There is also AnyEvent::Util::fork_call which is quite similar to what you want to do, and you may be able to adapt its methods to what you want to do while still leaving much of the heavy lifting to AnyEvent.

Note that 100 processes being kicked off can do serious damage to your system's responsiveness. Hopefully you have the requisite CPU/RAM :-)


Comment on Re: Parallel::ForkManager How to wait when 1st 10 Child processe are over out of 100
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://942524]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2014-04-19 09:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (480 votes), past polls