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
ARGV
. Yet it also shows constantly updated UI on progress (via repaint
). How?pids
hash to store the execution state of each process (working
or done
)pids
hash, it traps the CHLD
signal, which is the signal that occurs when a child process finishes. The
code also has to deal with the complication that kernel batches child signals
waitpid(-1)
.:done
in the pids
hashWHOHANG
argument to waitpid
is used to get waitpid
to return nil
immediately if no processes finished, instead of waiting as is the default.pids
have status of done.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