-
Notifications
You must be signed in to change notification settings - Fork 315
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
Add sample code of NET::HTTPResponse#read_body #1457
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
一度に読み出す場合は#bodyを使えばいいと思いますので、ブロックを渡して少しずつ読み出すサンプルがいいのかもしれません。
仮にこうするとした時にNet::HTTP.get_response的に一点気になるのはコネクションはcloseされてると思うので、read_bodyで少しずつ読み出すのが既にメモリ上のデータかどうか(全部結果受け取り終わってるかどうか)がまだよくわかってない事ですね。 例えばイメージファイルなどの大きいデータを取得して少しずつファイルに落とすサンプルがいい?と思った訳なのですが、その際にはメモリをあまり食わない、全部書き出せる、コネクションが正しく閉じれる内容だと嬉しいなと。ですが、get_responseで返るNet::HTTPResponseを操作するのはNet::HTTP#startのブロックの中でやるのがいいかどうかがまだ自分的にわかってないという…後で試してみるかもしれません。 |
rdoc にあったので、以下を作ってみました。メモリは取得するファイルのサイズよりはずっと小さい値しか消費しませんでした。.startのブロックの前後でFDの数を確認してみましたけど同じ数になりましたので、エラー処理など置いておくとこんなところですかねぇ。元のPRのサンプルは1つ目のサンプルとして、これを2つ目にするなどがいいかなと思いました。その場合、
get_responseのまま大きいファイルを読ませるとダウンロード完了まではread_bodyのブロックに入らないままでした。(消費メモリがどんどん増えていく) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2つめはマージ後に自分でやればよかったのでそうしようかと思います。
その後特にないようですのでマージしました。ありがとうございましたー。(Rakefileの問題でCIに怒られてましたけど、マージ後は解消済みかつ通ってました |
…se_i_read_body Add Net::HTTPResponse#read_body second example. (see gh-1457)
#433