Created
October 20, 2009 02:33
-
-
Save tansengming/213941 to your computer and use it in GitHub Desktop.
How not to use fork
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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