Skip to content

Instantly share code, notes, and snippets.

@tansengming
Created October 20, 2009 02:33
Show Gist options
  • Select an option

  • Save tansengming/213941 to your computer and use it in GitHub Desktop.

Select an option

Save tansengming/213941 to your computer and use it in GitHub Desktop.
How not to use fork
# Doing this will likely kill your machine
very_large_array.each do |array_item|
fork do
cpu_intensive_task(array_item)
end
end
# This however will lower the changes of that happening.
CPU_CORE_COUNT = 4
very_large_array.each_slice(CPU_CORE_COUNT).each do |smaller_array|
smaller_array.each do |array_item|
fork do
cpu_intensive_task(array_item)
end
end
Process.waitall
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment