How to keep track of process progress without hanging

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the unix category.

Last Updated: 2024-11-23

pids = {}

trap("CHLD") do
  while pids.values.include?(:working) &&
        pid = Process.waitpid(-1, Process::WNOHANG)
    pids[pid] = :done
  end
end

start_time = Time.now
FileUtils.mkpath "previews"
ARGV.each do |input_file|
  command = avconv_preview_command(input_file)
  pid = Process.spawn(command)
  pids[pid] = :working
end

until pids.values.all?{|v| v == :done}
  # repaint not shown
  repaint(pids, start_time)
  sleep 0.1
end

Resources