Skip to content

Instantly share code, notes, and snippets.

@ismasan
Forked from markevans/async_fibers.rb
Created November 17, 2011 15:00
Show Gist options
  • Save ismasan/1373324 to your computer and use it in GitHub Desktop.
Save ismasan/1373324 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