Skip to content
Myr

Document contextlib.closing.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Cory Benfield · committed by Cory Benfield · parent 0465d34 · Browse files · 1 file+14 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+14 added−0 removed

  • other:+14 added−0 removed

1 file+14 added−0 removed

modified:docs/user/advanced.rst+14 added
197 unmodified lines
198 ...199200You can further control the workflow by use of the :class:`Response.iter_content <requests.Response.iter_content>` and :class:`Response.iter_lines <requests.Response.iter_lines>` methods. Alternatively, you can read the undecoded body from the underlying urllib3 :class:`urllib3.HTTPResponse <urllib3.response.HTTPResponse>` at :class:`Response.raw <requests.Response.raw>`.201202If you set ``stream`` to ``True`` when making a request, Requests cannot203release the connection back to the pool unless you consume all the data or call204:class:`Response.close <requests.Response.close>`. This can lead to205inefficiency with connections. If you find yourself partially reading request206bodies (or not reading them at all) while using ``stream=True``, you should207consider using ``contextlib.closing`` (`documented here`_), like this::208209 from contextlib import closing210211 with closing(requests.get('http://httpbin.org/get', stream=True)) as r:212 # Do things with the response here.213214.. _`documented here`: http://docs.python.org/2/library/contextlib.html#contextlib.closing215216217Keep-Alive218----------219220Excellent news — thanks to urllib3, keep-alive is 100% automatic within a session! Any requests that you make within a session will automatically reuse the appropriate connection!
460 unmodified lines