mocker.org

stuck between web 2.0 and plain text

Batching of Forked Processes in Ruby

Sometimes the fun in not being a full-time (or good) programmer is coming up with odd solutions to problems that a real programmer would scoff/laugh at. Recently I had to devise a way to power through a huge number of processes in one of my scripts, without fork bombing my system. I came up with using the modulus operator and Process.waitall to force my script to halt after a batch of jobs started.

batch_incrementor = 0
batch_size = 10

50.times do |i|

  fork do
    puts i
    sleep 10
  end
  
  batch_incrementor += 1
  
  if (batch_incrementor/batch_size.to_f)%1 == 0.0
    Process.waitall
  end
  
end

I’m sure this method isn’t original or new, just new to me. :)

If any real programmers read this and have better ways, please let me know! I’m always ready to learn more.

Comments