Skip to content

Instantly share code, notes, and snippets.

@akdubya
Created April 23, 2009 23:15
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 akdubya/100828 to your computer and use it in GitHub Desktop.
Save akdubya/100828 to your computer and use it in GitHub Desktop.
require 'eventmachine'
require 'fiber'
def run_fiber(f)
EM.next_tick do
f.resume
if f.alive?
run_fiber(f)
end
end
end
def run_timed_fiber(f, interval)
EM.add_timer(interval) do
f.resume
if f.alive?
run_timed_fiber(f, interval)
end
end
end
EM.run do
puts 'starting main...'
f = Fiber.new {
10.times do |i|
puts i # A unit of work
Fiber.yield # Yield early and often
end
}
f2 = Fiber.new {
10.times do |i|
puts i
Fiber.yield
end
}
run_fiber(f)
run_timed_fiber(f2, 0.1)
puts 'continuing main...'
EM.add_timer(2) { EM.stop }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment