Skip to content

Instantly share code, notes, and snippets.

@adis-io
Created December 12, 2017 16:29
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 adis-io/dbfad7c83ae8e8a16019ba791e3899e5 to your computer and use it in GitHub Desktop.
Save adis-io/dbfad7c83ae8e8a16019ba791e3899e5 to your computer and use it in GitHub Desktop.
github.com/forkbreak/fork_break - usage example
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
gem 'fork_break'
end
process1, process2, process3 = Array.new(3) do |i|
ForkBreak::Process.new do |breakpoints|
random = Random.rand(1.0)
puts "sleep for #{random}"
sleep(random)
breakpoints << :after_sleep_1
puts "Process: #{i}"
puts Time.now.strftime('%Y-%m-%d %H:%M:%S.%3N')
sleep(2)
breakpoints << :after_sleep_2
end
end
process1.run_until(:after_sleep_1).wait
process2.run_until(:after_sleep_1).wait
process3.run_until(:after_sleep_1).wait
process1.finish
process2.finish
process3.finish.wait
@adis-io
Copy link
Author

adis-io commented Dec 12, 2017

Approx. output:

sleep for 0.36983955185930006
Process: 0
2017-12-12 22:30:20.539
sleep for 0.42565121857340893
Process: 2
2017-12-12 22:30:20.539
sleep for 0.8146626645464919
Process: 1
2017-12-12 22:30:20.539
[Finished in 4.0s]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment