Skip to content

Instantly share code, notes, and snippets.

@markevans
Created November 17, 2011 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save markevans/1373315 to your computer and use it in GitHub Desktop.
Save markevans/1373315 to your computer and use it in GitHub Desktop.
Async server example with Fibers
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