Created
October 18, 2012 11:00
-
-
Save Paxa/3911022 to your computer and use it in GitHub Desktop.
Async request handling with sinatra and EM
This file contains hidden or 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
#!/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