簡體   English   中英

如何從 Sinatra 配置瘦 web 服務器?

[英]How to configure Thin web server from Sinatra?

這是我的網絡應用程序:

class Front < Sinatra::Base
  configure do
    set :server, :thin
  end
  get '/' do
    'Hello, world!'
  end 
end

我是這樣開始的:

Front.start!

效果很好,但我想將 Thin 配置為“線程化”。 根據他們的文檔,我知道這是可能的。 但我不知道如何將threaded: true參數傳遞給 Thin。 我試過這個,但它不起作用:

configure do
  set :server_settings, threaded: true
end

默認情況下,瘦 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!

此答案中有關於 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!

可以在此處此處此處找到有關server_settings更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM