![]() |
|
Your skill will accomplish what the force of many cannot |
|
PerlMonks |
how to limit child processes?by Galen (Beadle) |
on May 02, 2001 at 01:37 UTC ( [id://77140]=perlquestion: print w/replies, xml ) | Need Help?? |
Galen has asked for the wisdom of the Perl Monks concerning the following question:
I'm using fork() and having trouble limiting the number of child processes that spawn. Say I have 12 tasks to do in a list. My technique is to to set a batch size ($children = 5) and process the list one by one, counting as I go along. If I've reached the batch size ($counter mod $children becomes false) or I'm at the end of the list, then go ahead and execute on this batch. Executing involves forking out $children number of child processes that take 15-20 seconds each to complete.
The problem is that even though the logical order of my code says to: 1) create the batch, then 2) do something with this batch, then 3) create another batch, it isn't working in that order. Instead, I watch as 12 child processes go out and do their thing. Even though part of the duty of each process is to print data and that data has not yet been returned to me, the loop figures it's done and proceeds to the next batch. I don't get it. Here's an example - shouldn't this snippet send off 5 processes which sleep for 15 seconds, then print 5 values, after which another 15 seconds passes and another 5 values are printed, then 15 seconds and the last two are printed? Instead, it sleeps for 15 seconds and prints all 12 values.
By the way, I tried using waitpid() with no success. Perhaps I was using it incorrectly. Is waitpid() the way to go, or is my logic just totally wrong?
Back to
Seekers of Perl Wisdom
|
|