Skip to content

Instantly share code, notes, and snippets.

@Paxa
Created October 18, 2012 11:00
Show Gist options
  • Save Paxa/3911022 to your computer and use it in GitHub Desktop.
Save Paxa/3911022 to your computer and use it in GitHub Desktop.
Async request handling with sinatra and EM
#!/usr/bin/env rackup -Ilib:../lib -s thin
# async message handling
# using gem https://github.com/raggi/async_sinatra
require 'sinatra/async'
module Handler
extend self
@pool = []
def <<(value)
@pool << value
end
def run
return if @running
@running = true
EventMachine::PeriodicTimer.new(1) do
if msg = @pool.shift
puts "Hamdler got message #{msg}"
EM.system("say #{msg}") do |o, status|
puts "Say command finished with sataus #{status}"
end
end
end
puts "Handler started"
end
end
class AsyncTest < Sinatra::Base
register Sinatra::Async
enable :show_exceptions
before do
Handler.run
end
aget '/:msg' do |msg|
body "hello async"
Handler << msg
end
end
run AsyncTest.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment