这是我的网络应用程序:
class Front < Sinatra::Base
configure do
set :server, :thin
end
get '/' do
'Hello, world!'
end
end
我是这样开始的:
Front.start!
效果很好,但我想将Thin配置为“线程化”。根据{{3}},我知道这是可能的。但是我不知道如何将threaded: true
参数传递给Thin。我试过了,但不起作用:
configure do
set :server_settings, threaded: true
end
答案 0 :(得分:2)
按您所描述的方式启动时,默认情况下,瘦Web服务器是线程化的。
# 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!
开始于:
ruby thin_test.rb
确认:
# 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!
this answer中还有更多有关Sinatra如何使用其他Web服务器的细节。
如果由于某种原因该方法不起作用,则可能可以使用server_settings
选项将某些东西一起砍掉,该选项通常仅对WEBrick有用,除非您使用一些未公开说明的方式对其进行强制:
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!
我很难说出这个示例是否是线程化的原因,因为默认情况下它是在线程模式下启动的。也就是说,它不会引发异常,并且可以在线程模式下运行:
# 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!