Search code examples
rubymultithreadingsinatrathin

Sinatra with thin, multi thread do not work


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'

Solution

  • 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.