HTTParty的内存问题和下载大文件 [英] Memory issues with HTTParty and download large files

查看:71
本文介绍了HTTParty的内存问题和下载大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是否会导致Ruby出现内存问题.我知道,如果大小超过10KB,则Open-URI会写入TempFile.但是HTTParty是否会在将整个PDF写入TempFile之前尝试将其保存到内存中?

Is this going to cause memory issues with Ruby. I know Open-URI writes to a TempFile if the size goes over 10KB. But will HTTParty try and save the whole PDF to memory before it writes to TempFile?

src = Tempfile.new("file.pdf")
src.binmode
src.write HTTParty.get("large_file.pdf").parsed_response

推荐答案

您可以使用Net :: HTTP.请参见文档(在特别是标题为流式响应实体"的部分).

You can use Net::HTTP. See the documentation (in particular the section titled "Streaming Response Bodies").

这是文档中的示例:

uri = URI('http://example.com/large_file')

Net::HTTP.start(uri.host, uri.port) do |http|
  request = Net::HTTP::Get.new uri.request_uri

  http.request request do |response|
    open 'large_file', 'w' do |io|
      response.read_body do |chunk|
        io.write chunk
      end
    end
  end
end

这篇关于HTTParty的内存问题和下载大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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