Created
August 21, 2020 15:53
-
-
Save mehmetaydogduu/8324700f76087143be2bb857fa98eed1 to your computer and use it in GitHub Desktop.
Ruby Async
This file contains 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
require 'async' | |
require 'active_support/all' | |
def sleepy(id) | |
Async do |task| | |
while true | |
task.sleep 0.5 | |
puts "I'm #{id}!" | |
end | |
end | |
end | |
t = Time.now | |
stopped = false | |
Async do | |
a = sleepy(1) | |
b = sleepy(2) | |
stopper = Async do |task| | |
while !stopped | |
task.sleep 0.5 | |
if Time.now > t + 5.seconds && !stopped | |
a.stop | |
b.stop | |
stopped = true | |
binding.irb | |
end | |
end | |
end | |
end | |
puts "Finally stopped all async and go functional" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment