The thin web server is threaded by default when started in the way you describe.
# thin_test.rb
require 'sinatra/base'
class Front < Sinatra::Base
configure do
set :server, :thin
end
get '/' do
'Hello, world!'
end
get '/foo' do
sleep 30
'bar'
end
end
Front.start!
Start with:
ruby thin_test.rb
Confirm with:
# will hang for 30 seconds while sleeping
curl localhost:4567/foo
# will complete successfully while the other request is pending
curl localhost:4567
Hello, world!
There is additional detail in this answer about how Sinatra uses other web servers.
If this doesn't work for some reason, it may be possible to hack together something with the server_settings
option, which is generally only useful for WEBrick unless you use some undocumented ways to force it:
require 'sinatra/base'
require 'thin'
class ThreadedThinBackend < ::Thin::Backends::TcpServer
def initialize(host, port, options)
super(host, port)
@threaded = true
end
end
class Front < Sinatra::Base
configure do
set :server, :thin
class << settings
def server_settings
{ :backend => ThreadedThinBackend }
end
end
end
get '/' do
'Hello, world!'
end
get '/foo' do
sleep 30
'foobar'
end
end
Front.start!
It's hard for me to tell if this example is the cause of it being threaded though, because it starts in threaded mode by default. That said, it doesn't raise an exception and does run in threaded mode:
# will hang for 30 seconds while sleeping
curl localhost:4567/foo
# will complete successfully while the other request is pending
curl localhost:4567
Hello, world!
More information about server_settings
can be found here, here, and here.