Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sample code of NET::HTTPResponse#read_body #1457

Merged

Conversation

tbpgr
Copy link
Contributor

@tbpgr tbpgr commented Sep 28, 2018

Copy link
Member

@sho-h sho-h left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一度に読み出す場合は#bodyを使えばいいと思いますので、ブロックを渡して少しずつ読み出すサンプルがいいのかもしれません。

@sho-h
Copy link
Member

sho-h commented Oct 11, 2018

一度に読み出す場合は#bodyを使えばいいと思いますので、ブロックを渡して少しずつ読み出すサンプルがいいのかもしれません。

仮にこうするとした時にNet::HTTP.get_response的に一点気になるのはコネクションはcloseされてると思うので、read_bodyで少しずつ読み出すのが既にメモリ上のデータかどうか(全部結果受け取り終わってるかどうか)がまだよくわかってない事ですね。

例えばイメージファイルなどの大きいデータを取得して少しずつファイルに落とすサンプルがいい?と思った訳なのですが、その際にはメモリをあまり食わない、全部書き出せる、コネクションが正しく閉じれる内容だと嬉しいなと。ですが、get_responseで返るNet::HTTPResponseを操作するのはNet::HTTP#startのブロックの中でやるのがいいかどうかがまだ自分的にわかってないという…後で試してみるかもしれません。

@sho-h
Copy link
Member

sho-h commented Oct 14, 2018

get_responseで返るNet::HTTPResponseを操作するのはNet::HTTP#startのブロックの中でやるのがいいかどうかがまだ自分的にわかってないという…後で試してみるかもしれません。

rdoc にあったので、以下を作ってみました。メモリは取得するファイルのサイズよりはずっと小さい値しか消費しませんでした。.startのブロックの前後でFDの数を確認してみましたけど同じ数になりましたので、エラー処理など置いておくとこんなところですかねぇ。元のPRのサンプルは1つ目のサンプルとして、これを2つ目にするなどがいいかなと思いました。その場合、 @see で Net::HTTP#request_get へのリンクも貼っておきたいところですね。

#@samplecode 例2 大きいファイルを取得
require 'net/http'

uri = URI.parse('http://www.example.com/path/to/big.file')
Net::HTTP.start(uri.host, uri.port) do |http|
  # Net::HTTP#request_get と Net::HTTPResponse#read_body で少しずつ読み込みながら書き出し
  http.request_get(uri.path) do |response|
    File.open("/path/to/big.file", "w") do |f|
      response.read_body do |s|
        f.write(s)
      end
    end
  end
end
#@end

get_responseのまま大きいファイルを読ませるとダウンロード完了まではread_bodyのブロックに入らないままでした。(消費メモリがどんどん増えていく)

Copy link
Member

@sho-h sho-h left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2つめはマージ後に自分でやればよかったのでそうしようかと思います。

@sho-h sho-h merged commit d5676f1 into rurema:master Oct 20, 2018
sho-h added a commit to sho-h/doctree that referenced this pull request Oct 21, 2018
@sho-h
Copy link
Member

sho-h commented Oct 21, 2018

その後特にないようですのでマージしました。ありがとうございましたー。(Rakefileの問題でCIに怒られてましたけど、マージ後は解消済みかつ通ってました

@tbpgr tbpgr deleted the add_sample_code_of_nethttpresponse_i_read_body branch October 21, 2018 11:32
sho-h added a commit that referenced this pull request Oct 27, 2018
…se_i_read_body

Add Net::HTTPResponse#read_body second example. (see gh-1457)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants