在EventMachine上运行时,Thin会生成工作者

时间:2012-11-27 17:17:07

标签: ruby eventmachine thin

我有一个基于Sinatra基础的ruby web- / data-server应用程序。我正在添加EventMachine和em-websocket,以便我可以拥有异步数据传输连接。

#! /usr/bin/env ruby

require 'yajl'
require 'json'
require 'em-websocket'
require 'sinatra/base'
require 'thin'
require './Actors/DataProvider.rb'

my_host = '10.2.56.87'
my_web_port = '4004'
my_web_sock = '8008'

EventMachine.epoll  # choose kernel epoll over select for deterministic execution under load
EventMachine.run do

   class SinatraApp < Sinatra::Base

    configure do
      set :bind, '10.2.56.87'
      set :port, '4004'
    end

    encoder = Yajl::Encoder.new
    communicator = DataProvider.new( encoder )

    get '/' do
      redirect '/index-work.html'
    end

    before do
      content_type 'application/json'
    end

    post '/auth' do
    end

    post '/data/:id' do
      puts 'got d i '
    end
  end
  EventMachine::WebSocket.start( :host => my_host, :port => my_web_sock ) do | ws |
    ws.onopen do

    end
    ws.onmessage do

    end
    ws.onclose do

    end
  end
  begin
    SinatraApp.run!
  rescue => ex
    puts "#{ ex.class}: #{ ex.message }"
  end
end

瘦服务器在服务Sinatra时运行良好,但现在我已经添加了EM基础,发生了以下情况:

/usr/local/rvm/gems/ruby-1.9.3-p286/gems/thin-2.0.0.pre/lib/thin/server.rb:192:in `block (2 levels) in start': undefined method `attach_server' for EventMachine:Module (NoMethodError)
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/thin-2.0.0.pre/lib/thin/server.rb:191:in `each'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/thin-2.0.0.pre/lib/thin/server.rb:191:in `block in start'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/thin-2.0.0.pre/lib/thin/backends/prefork.rb:30:in `block (2 levels) in start'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `call'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run_machine'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/thin-2.0.0.pre/lib/thin/backends/prefork.rb:23:in `block in start'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/preforker-0.1.1/lib/preforker/worker.rb:52:in `call'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/preforker-0.1.1/lib/preforker/worker.rb:52:in `work'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/preforker-0.1.1/lib/preforker.rb:129:in `block (2 levels) in spawn_missing_workers'
from (eval):6:in `block in fork'
from (eval):6:in `fork'
from (eval):6:in `fork'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/preforker-0.1.1/lib/preforker.rb:126:in `block in spawn_missing_workers'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/preforker-0.1.1/lib/preforker.rb:124:in `times'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/preforker-0.1.1/lib/preforker.rb:124:in `spawn_missing_workers'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/preforker-0.1.1/lib/preforker.rb:44:in `run'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/thin-2.0.0.pre/lib/thin/backends/prefork.rb:37:in `start'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/thin-2.0.0.pre/lib/thin/server.rb:187:in `start'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/rack-1.4.1/lib/rack/handler/thin.rb:13:in `run'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/sinatra-1.3.3/lib/sinatra/base.rb:1350:in `run!'
from /home/devel/ISF_Server/server-work.rb:122:in `block in <top (required)>'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `call'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run_machine'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run'
from /home/devel/ISF_Server/server-work.rb:15:in `<top (required)>'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/ruby-debug-ide-0.4.17.beta14/lib/ruby-debug-ide.rb:127:in `debug_load'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/ruby-debug-ide-0.4.17.beta14/lib/ruby-debug-ide.rb:127:in `debug_program'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/ruby-debug-ide-0.4.17.beta14/bin/rdebug-ide:118:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'         

我注意到Thin gem最近更新了,但EM还没有。这是我的下一个询问点。其他人发布Thin不应该从EM.run块中运行,但我正在构建的示例是这样的。

1 个答案:

答案 0 :(得分:1)

好。我直接与Marc-Andre Cournoyer通信。他说,首先,不要在EM.run块中产生This(通过Sinatra)。

其次,错误是从Thin 2(pre)生成的,需要EventMachine live Edition或gem eventmachine-le。

现在,他好了。 :d