Skip to content

Instantly share code, notes, and snippets.

@aldrinmartoq
Created August 28, 2018 04:06
Show Gist options
  • Save aldrinmartoq/6e1a28ea50fba9bf14735f12275b45ce to your computer and use it in GitHub Desktop.
Save aldrinmartoq/6e1a28ea50fba9bf14735f12275b45ce to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
module ForkEach
def fork_each(max = 4)
pids = []
self.each do |item|
pids << fork { yield item }
pids.delete(Process.wait 0) if pids.length >= max
end
pids.each { |pid| Process.wait pid }
end
end
Array.send(:include, ForkEach)
Range.send(:include, ForkEach)
# código que convierte archivos en paralelo
require 'find'
prc = 4
cmd = "/Applications/calibre.app/Contents/MacOS/ebook-convert"
dir = '/Volumnes/DISCO'
ini = /\.fb2$/
fin = '.epub'
items = Find.find(dir).select do |path|
next unless path =~ ini && File.file?(path)
result = !File.exist?(path.sub(ini, fin))
puts result ? "process #{path}" : "skipping #{path}"
result
end
print "ENTER para continuar, CTRL+C para cancelar "
gets
items.fork_each(prc) do |src|
dst = src.sub(ini, fin)
puts "#{cmd} #{src} → #{dst}"
exec(cmd, src, dst)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment