歌利亚不是异步的

时间:2014-02-19 17:15:46

标签: ruby eventmachine goliath

我在我的localhost上使用Ruby 1.9.3运行一个简单的goliath服务器,并且它没有异步运行http请求。这是代码:

require 'goliath'
require 'em-synchrony'
require 'em-synchrony/em-http'

class Server < Goliath::API
  use Goliath::Rack::Validation::RequestMethod, %w(GET PUT POST)

  def initialize
    super
    puts "Started up Bookcover server... Let 'em come!"
  end

  def response(env)
    thumbnail_cover_url, large_book_cover_url = ["http://riffle-bookcovers.s3.amazonaws.com/B00GJYXA5I-thumbnail.jpg", "http://riffle-bookcovers.s3.amazonaws.com/B00GJYXA5I-original.jpg"]
    puts "start"
    a = EM::HttpRequest.new(thumbnail_cover_url).get
    b = EM::HttpRequest.new(large_book_cover_url).get
    puts "done"
    [200, {}, "Hello World"]
  end
end

当我运行ab -n 100 http://127.0.0.1:9000/时,我可以看到它等待每个请求完成,这意味着这些调用都是阻塞的。

然而,根据文档,Goliath使用Em-synchrony让我编写“看起来同步”的代码,这不是这里的情况。

我很感激任何提示和评论!

1 个答案:

答案 0 :(得分:0)

致谢igrigorik获取答案。我引用他的回答here

  

您还需要在运行ab时指定并发级别...例如   ab -c 10:)

     

另外,请确保以生产模式(-e prod)运行它。