Created
April 23, 2009 23:15
-
-
Save akdubya/100828 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 '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