質問

I have done a simple test on my Sinatra app, when I call the long handler, the dummy request is blocked.

 get '/test/long' do
    sleep 10
    "finished"
 end 

 get '/test/dummy' do
    "dummy"

 end

I started my server using this command:

bundle exec rackup -s thin

According to Is Sinatra multi threaded?, Thin should be a multi thread web server. So what is my problem here?

my Gemfile:
source :rubyforge
gem 'sinatra',           '1.2.6', :require => 'sinatra/base'

gem 'geokit',        '1.6.0', :require => 'geokit'
gem 'json',              '1.5.3'
gem 'dm-core',           '1.2.0'
gem 'dm-timestamps',     '1.2.0'
gem 'dm-migrations',     '1.2.0'
gem 'dm-mysql-adapter',  '1.2.0'
gem 'rack-cache',        '1.0.1', :require => 'rack/cache'
gem 'rake',              '10.0.0',  :require => nil
gem 'hashie',            '1.0.0'
gem 'thin'
gem 'shotgun'
gem 'rack-mobile-detect', '0.3.0', :require => 'rack/mobile-detect'
gem 'aws-ses',                     :require => 'aws/ses'
役に立ちましたか?

解決

Thin can be multi-threaded, but only if you configure it to be so, by default it is single-threaded (evented). From the answer to the question you linked to:

since Sinatra 1.3.0, Thin will be started in threaded mode, if it is started by Sinatra (i.e. with ruby app.rb, but not with the thin command, nor with rackup).

There doesn’t appear to be a way to get rackup to pass the threaded option through to Thin, so you will need to use either thin start --threaded or ruby my_app.rb to get threading on Thin.

他のヒント

Here is a way to do this in Rack::Handler with out Sinatra. The 'run' method takes a block parameter, which gives you access to Thin::Server object and you can set all parameters through it.

  require 'thin'
  require 'rack'

  class App
            def initialize()
               puts 'init'
    end

    def call(env)
            puts 'got it'
            sleep(60)
            [ 200, { "Content-Type" => "text/plain" }, ["hello"] ]
    end

  end

  Rack::Handler::Thin.run(App.new, options = {:Host => '0.0.0.0', :Port => 8083}) do |server|
       puts server.class
       server.threaded = true
  end
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top