Created
November 17, 2011 14:55
-
-
Save markevans/1373315 to your computer and use it in GitHub Desktop.
Async server example with Fibers
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 'fiber' | |
module Stuff | |
class << self | |
def system_call_1 | |
f = Fiber.current | |
EM.system 'delayedEcho dogs' do |result, status| | |
f.resume result | |
end | |
Fiber.yield | |
end | |
def system_call_2 | |
f = Fiber.current | |
EM.system 'delayedEcho pigs' do |result, status| | |
f.resume result | |
end | |
Fiber.yield | |
end | |
end | |
end | |
class DragonflyServer | |
def call(env) | |
Fiber.new do | |
res1 = Stuff.system_call_1 | |
res2 = Stuff.system_call_2 | |
env['async.callback'].call [200, {'Content-Type' => 'text/plain'}, [res1, res2]] | |
end.resume | |
[-1, {}, []] | |
end | |
end | |
run DragonflyServer.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment