使用Thin和Sinatra异步迭代请求的响应 [英] Asynchronously iterating over the response of a request using Thin and Sinatra

查看:201
本文介绍了使用Thin和Sinatra异步迭代请求的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你在Sinatra的回应返回一个'eachable'对象,Sinatra的事件循环将'每个'你的结果,并以流式方式作为HTTP响应产生结果。但是,如果存在对Sinatra的并发请求,它将在处理另一个请求之前迭代一个响应的所有元素。如果我们有一个光标到一些DB查询的结果,这意味着我们必须等待所有的数据可用之前处理并发查询。

If your response in Sinatra returns an 'eachable' object, Sinatra's event loop will 'each' your result and yield the results in a streaming fashion as the HTTP response. However, if there are concurrent requests to Sinatra, it will iterate through all the elements of one response before handling another request. If we have a cursor to the results of some DB query, that means we have to wait for all the data to be available before handling a concurrent query.

查看了async-sinatra gem和 http:// macournoyer.com/blog/2009/06/04/pusher-and-async-with-thin/ ,认为这些会解决我的问题,但我已经尝试了这个例子:

I've looked at the async-sinatra gem and http://macournoyer.com/blog/2009/06/04/pusher-and-async-with-thin/, thinking these would solve my problem, but I've tried out this example:

require 'sinatra/async'

class AsyncTest < Sinatra::Base
  register Sinatra::Async

  aget '/' do
    body "hello async"
  end

  aget '/delay/:n' do |n|
    EM.add_timer(n.to_i) { body { "delayed for #{n} seconds" } }
  end
end

/ delay / 5 请求不能同时工作,

and the /delay/5 request doesn't work concurrently as I expect it to, i.e. I make 3 requests concurrently and Chrome's debugger notes the response times as roughly 5, 10, and 15 seconds.

我错过了一些设置,或者有另一种方法来告诉Sinatra / Thin,它的响应时间大约为5秒,10秒和15秒。

Am I missing some setup or is there another way to tell Sinatra/Thin to handle requests in a concurrent manner?

更新:这里是另一个扳手(或者可能清除的东西):
运行 curl - i http:// localhost:3000 / delay / 5 同时具有正确的行为(2个请求每次回来〜5秒)。运行 ab -c 10 -n 50 http:// locahost:3000 / delay / 5 < a> (Apache基准测试工具)也返回一个合理的总时间(〜25秒)。 Firefox的行为与Chrome相同。什么浏览器与命令行实用程序不同?

Update: Here's another wrench in this (or possibly clears things up): Running curl -i http://localhost:3000/delay/5 concurrently has the correct behavior (2 requests each come back in ~5 seconds). Running ab -c 10 -n 50 http://locahost:3000/delay/5 (the Apache benchmark utility) also returns something reasonable for the total time (~25 seconds). Firefox exhibits the same behavior as Chrome. What are the browsers doing different from the command-line utilities?

推荐答案

确实工作,我可以最终得到Sinatra流并发每个结果,主要使用 EM.defer 在Pusher和Async页面的想法。 curl和Apache基准测试确认这是正常工作。

So in the end, I found out that the example did indeed work and I could eventually get Sinatra to stream each-able results concurrently, primarily using the EM.defer idea in Pusher and Async page. curl and Apache benchmarking confirmed that this was working.

它在浏览器中没有工作的原因是因为浏览器限制连接到同一个网址的数量。我知道有并发连接到单个域的限制(也是一个低数字),但不是(似乎)所有到单个URI的连接序列化:

The reason why it didn't work in the browser is because browsers limit the number of connections to the same URL. I was aware of there being a limit to concurrent connections to a single domain (also a low number), but not that (seemingly) all connections to a single URI are serialized:

http://maillist.caucho.com/pipermail/resin -interest / 2009-August / 003998.html

我不知道这是否可配置,我只在Firefox中看到域范围的配置,这是问题。

I don't know if this is configurable, I only see domain-wide configuration in Firefox, but that was the issue.

这篇关于使用Thin和Sinatra异步迭代请求的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆