【问题标题】:"uncaught throw :async" when using async_sinatra with Rails EventMachine and Thin将 async_sinatra 与 Rails EventMachine 和 Thin 一起使用时出现“未捕获的 throw :async”
【发布时间】:2011-11-30 13:28:17
【问题描述】:

我正在尝试设置我的环境以让 Rails、EventMachine 和 async_sinatra 一起玩,我想我的设置有误,因为我收到错误“未捕获的 throw :async”。

这是我的 Gemfile 中的内容:

source 'http://rubygems.org'

gem 'rails', '3.1.3'
gem 'sinatra'
gem 'async_sinatra'
gem 'execjs'
gem 'therubyracer'
gem 'eventmachine'
gem 'rack-fiber_pool',  :require => 'rack/fiber_pool'
gem 'thin'

这是 config.ru:

require ::File.expand_path('../config/environment',  __FILE__)
use Rack::FiberPool
run Longpoll::Application

这是我在 routes.rb 文件中的内容:

match '/longpoll', :to => LongPoll

这是我的 long_poll.rb 文件:

class LongPoll < Sinatra::Base
  register Sinatra::Async

  aget '/longpoll' do
    body 'Hello world!'
  end
end

任何想法我缺少什么让这个设置工作?

编辑:

我发现不使用 rack-fiber_pool 可以解决问题。 谁能确认他们是否可以一起工作?

另一个给出相同错误basic.ru的例子:

#!/usr/bin/env rackup -Ilib:../lib -s thin
require 'sinatra/async'
require 'rack/fiber_pool'

class AsyncTest < Sinatra::Base
  register Sinatra::Async
  use Rack::FiberPool

  enable :show_exceptions

  aget '/' do
    body "hello async"
  end
end

run AsyncTest.new

【问题讨论】:

    标签: ruby-on-rails sinatra eventmachine thin


    【解决方案1】:

    如果在使用webrick 时看到此错误,切换到thin 即可解决。 因此我无法直接确认您的假设,但我想使用webrickrack-fiber_pool 可能会中断。

    #!/usr/bin/env ruby
    #
    require "rubygems"
    require "sinatra/async"
    
    class AsyncTest < Sinatra::Base
      set :server, :thin
    
      register Sinatra::Async
    
      aget '/' do
        body "hello async"
      end
    
      run! if app_file == $0
    end
    

    上面的代码对我来说很好。

    【讨论】:

      【解决方案2】:

      IMO 它们是相互排斥的。

      rack-fiber-pool 负责使用光纤结果调用async.callback。它还负责做一个throw :async 告诉瘦保持连接打开。 async_sinatra 也处理完全相同的事情。当您调用 body {} 时,它会调用 async.callback。并且还做了一个throw :async 认为它必须告诉薄以保持连接打开。这是您看到的问题。 async_sinatra 正在抛出 :async 并且 rack-fiber-pool 正在捕获它并认为这是一个实际错误。

      您可以在这里查看 async_sinatra 投掷:https://github.com/raggi/async_sinatra/blob/master/lib/sinatra/async.rb#L69

      和 rack-fiber-pool 在这里捕获:https://github.com/mperham/rack-fiber_pool/blob/master/lib/rack/fiber_pool.rb#L24

      如果您想要两者兼得,也可以查看 sinatra_synchrony。

      【讨论】:

        猜你喜欢
        • 2012-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-05
        • 1970-01-01
        • 2021-01-20
        • 1970-01-01
        相关资源
        最近更新 更多