Skip to content

Instantly share code, notes, and snippets.

@ranjib
Created March 20, 2014 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ranjib/9670069 to your computer and use it in GitHub Desktop.
Save ranjib/9670069 to your computer and use it in GitHub Desktop.
# process based .. has memory implication
sleep_time = 10
pids= []
10.times do |n|
pids << fork do
sleep sleep_time
end
sleep_time += 10
end
# not exactly parallel,, this will halt the execution till the pid returns (im sure Process class has some async api, to get around this)
pids.each do |pid|
Process.waitpid(pid)
end
# thread based
sleep_time = 10
threads = []
10.times do |n|
threads << Thread.new do
sleep sleep_time
end
sleep_time += 10
end
pids.each do |thread|
thread.join
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment