Memory issues with HTTParty and download large files

时光毁灭记忆、已成空白 提交于 2019-11-29 14:57:51

问题


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

回答1:


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

Here's the example from the docs:

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


来源:https://stackoverflow.com/questions/9304141/memory-issues-with-httparty-and-download-large-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!