Rails 3.2 流媒体 [英] Rails 3.2 streaming

查看:51
本文介绍了Rails 3.2 流媒体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理来自 Rails 3.2 的流式下载 (CSV),并且遇到了初始页面请求需要很长时间的问题.以下控制器代码说明了我的问题:

I am working on a streaming download (CSV) from Rails 3.2 and am coming up against an issue of the initial page request taking a long time. The following controller code illustrates my issue:

      self.response_body = Enumerator.new do |y|
        10_000_000.times do
          y << "Hello World"
        end
      end 

有了上面的内容,响应确实看起来像是它的流媒体(来自可以支持它的服务器......在我的情况下是独角兽).也就是说,在它开始流式传输之前,它挂起的时间比我想要的要长得多.如果我将其更改为以下内容,它会启动得更快:

With the above, the response does seem like its streaming (from a server than can support it... Unicorn, in my case). That said, before it starts streaming it hangs for a much longer time than I'd like. If I change it to the following, it starts much faster:

      self.response_body = Enumerator.new do |y|
        1000.times do
          y << "Hello World"
        end
      end

我的理解是响应应该从循环的第一次迭代开始,但似乎较大的循环导致初始加载时间延长.如果每次迭代都在发生时输出,那么无论总共有多少次迭代,启动流处理过程不应该花费相同的时间吗???

My understanding is that the response should begin with the first iteration of the loop, but it seems the larger loops are causing that initial load time to lengthen. If each iteration is output as it happens, shouldn't it take the same amount of time to kick off the streaming process, regardless of how many total iterations there will be???

感谢您的任何见解!

这是对我正在尝试的技术的解释.也许我误解或遗漏了一步?:http://facebook.stackoverflow.com/questions/3507594/ruby-on-rails-3-streaming-data-through-rails-to-client/4320399#4320399

Here is an explanation of the technique I am attempting. Maybe I am misinterpreting or missing a step?: http://facebook.stackoverflow.com/questions/3507594/ruby-on-rails-3-streaming-data-through-rails-to-client/4320399#4320399

认为 Rack-Cache 可能导致我的问题...我可以针对单个请求关闭它吗?

I think Rack-Cache might be causing my problem... can I turn it off for an individual request?

编辑并解决:

我对 Rack-Cache 的看法是错误的.我只需要将 self.response.headers['Last-Modified'] = Time.now.ctime.to_s 添加到我的回复中.

I was wrong about Rack-Cache. i just needed to add self.response.headers['Last-Modified'] = Time.now.ctime.to_s to my response.

推荐答案

经过编辑的问题结果正好包含我需要的答案.将其发布在这里作为答案.

让 Rack 处理程序正确传输的答案显然是在响应中添加一个 Last-Modified 标头:

The answer to getting the Rack handler to stream properly is apparently to add a Last-Modified header to the response:

self.response.headers['Last-Modified'] = Time.now.ctime.to_s

这篇关于Rails 3.2 流媒体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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