Skip to content

Commit

Permalink
Properly wait between failed wait attempts to avoid busy loop
Browse files Browse the repository at this point in the history
  • Loading branch information
knu committed May 23, 2020
1 parent 788d95c commit cf3cce3
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/parallel/forkmanager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ def start(identification = nil, *args, &run_block)
while @max_procs.nonzero? && @processes.length >= @max_procs
on_wait
arg = (defined? @on_wait_period && !@on_wait_period.nil?) ? Process::WNOHANG : nil
wait_one_child(arg)
kid = wait_one_child(arg)
if kid == 0 || kid == -1
sleep @waitpid_blocking_sleep
end
end

wait_children
Expand Down Expand Up @@ -343,10 +346,13 @@ def wait_one_child(par)
# forked. This is a blocking wait.
#
def wait_all_children
until @processes.keys.empty?
until @processes.empty?
on_wait
arg = (defined? @on_wait_period and !@on_wait_period.nil?) ? Process::WNOHANG : nil
wait_one_child(arg)
kid = wait_one_child(arg)
if kid == 0 || kid == -1
sleep @waitpid_blocking_sleep
end
end
rescue Errno::ECHILD
# do nothing.
Expand Down

0 comments on commit cf3cce3

Please sign in to comment.