Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@clc3123
Created November 26, 2011 02:13
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 clc3123/1394840 to your computer and use it in GitHub Desktop.
Save clc3123/1394840 to your computer and use it in GitHub Desktop.
thin_async_app_example.ru
require "eventmachine"
class DeferrableBody
include EventMachine::Deferrable
def call(body)
body.each do |chunk|
@body_callback.call(chunk)
end
end
def each &blk
@body_callback = blk
end
end
class AsyncApp
def call(env)
body = DeferrableBody.new
EventMachine::next_tick {
env['async.callback'].call [200, {'Content-Type' => 'text/plain'}, body]
body.call ["Hey!\n"]
}
EventMachine::add_timer(2) {
body.call ["Woah, async!\n"]
EventMachine::add_timer(3) {
body.call ["Cheers then!"]
body.succeed
}
}
throw :async # Still works for supporting non-async frameworks...
end
end
run AsyncApp.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment