【问题标题】:Sinatra with thin, multi thread do not work带有细多线程的 Sinatra 不起作用
【发布时间】:2013-07-08 08:42:23
【问题描述】:

我在我的 Sinatra 应用程序上做了一个简单的测试,当我调用长处理程序时,虚拟请求被阻止。

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

 get '/test/dummy' do
    "dummy"

 end

我使用这个命令启动了我的服务器:

bundle exec rackup -s thin

根据Is Sinatra multi threaded?,Thin 应该是一个多线程 Web 服务器。那么我的问题是什么?

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'

【问题讨论】:

    标签: ruby multithreading sinatra thin


    【解决方案1】:

    Thin 可以是多线程的,但前提是您将其配置为多线程,默认情况下它是单线程的(事件)。来自answer to the question you linked to

    从 Sinatra 1.3.0 开始,如果 Thin 由 Sinatra 启动(即使用 ruby app.rb,但不是使用 thin 命令,也不是使用 rackup),它将以线程模式启动。

    似乎没有办法让rackup 将线程选项传递给Thin,因此您需要使用thin start --threadedruby my_app.rb 在Thin 上获得线程。

    【讨论】:

      【解决方案2】:

      这是一种在没有 Sinatra 的情况下在 Rack::Handler 中执行此操作的方法。 'run' 方法接受一个块参数,它使您可以访问 Thin::Server 对象,您可以通过它设置所有参数。

        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
      

      【讨论】:

        猜你喜欢
        • 2015-03-30
        • 2014-07-17
        • 1970-01-01
        • 1970-01-01
        • 2014-04-30
        • 2018-09-11
        • 1970-01-01
        • 1970-01-01
        • 2016-11-04
        相关资源
        最近更新 更多