Skip to content

Instantly share code, notes, and snippets.

@mehmetaydogduu
Created August 21, 2020 15:53
Show Gist options
  • Save mehmetaydogduu/8324700f76087143be2bb857fa98eed1 to your computer and use it in GitHub Desktop.
Save mehmetaydogduu/8324700f76087143be2bb857fa98eed1 to your computer and use it in GitHub Desktop.
Ruby Async
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