diff options
| author | Ed Morley <edmorley@users.noreply.github.com> | 2017-06-06 23:26:07 +0100 |
|---|---|---|
| committer | Ed Morley <edmorley@users.noreply.github.com> | 2017-06-06 23:30:47 +0100 |
| commit | 4847f5b8cd4071099f06e178856bc9e1b9193b2e (patch) | |
| tree | 17a2cef7f84df8273fa445b90e9a3221a27504fd /docs/user | |
| parent | 40ce8144d186ce4e3a06b3f3d59222199925e108 (diff) | |
| download | python-requests-4847f5b8cd4071099f06e178856bc9e1b9193b2e.tar.gz | |
Allow Requests.Response to be used as a context manager
This saves having to wrap the call to requests with
`contextlib.closing()`, allowing it to be used directly in a
`with` statement, like so:
```
with requests.get('http://httpbin.org/get', stream=True) as r:
# Do things with the response here.
```
Fixes #4136.
Diffstat (limited to 'docs/user')
| -rw-r--r-- | docs/user/advanced.rst | 8 |
1 files changed, 2 insertions, 6 deletions
diff --git a/docs/user/advanced.rst b/docs/user/advanced.rst index 2aac434c..a1b68707 100644 --- a/docs/user/advanced.rst +++ b/docs/user/advanced.rst @@ -301,15 +301,11 @@ release the connection back to the pool unless you consume all the data or call :meth:`Response.close <requests.Response.close>`. This can lead to inefficiency with connections. If you find yourself partially reading request bodies (or not reading them at all) while using ``stream=True``, you should -consider using ``contextlib.closing`` (`documented here`_), like this:: +make the request within a ``with`` statement to ensure it's always closed:: - from contextlib import closing - - with closing(requests.get('http://httpbin.org/get', stream=True)) as r: + with requests.get('http://httpbin.org/get', stream=True) as r: # Do things with the response here. -.. _`documented here`: http://docs.python.org/2/library/contextlib.html#contextlib.closing - .. _keep-alive: Keep-Alive |
