Skip to content
This repository was archived by the owner on Jan 13, 2021. It is now read-only.

Commit d586530

Browse files
committed
Connections are context managers.
1 parent 47d99a1 commit d586530

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

docs/source/advanced.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,27 @@ Advanced Usage
55

66
This section of the documentation covers more advanced use-cases for ``hyper``.
77

8+
Responses as Context Managers
9+
-----------------------------
10+
11+
If you're concerned about having too many TCP sockets open at any one time, you
12+
may want to keep your connections alive only as long as you know you'll need
13+
them. In HTTP/2.0 this is generally not something you should do unless you're
14+
very confident you won't need the connection again anytime soon. However, if
15+
you decide you want to avoid keeping the connection open, you can use the
16+
:class:`HTTP20Connection <hyper.HTTP20Connection>` as a context manager::
17+
18+
with HTTP20Connection('twitter.com:443') as conn:
19+
conn.request('GET', '/')
20+
data = conn.getresponse().read()
21+
22+
analyse(data)
23+
24+
You may not use any :class:`HTTP20Response <hyper.HTTP20Response>` objects
25+
obtained from a connection after that connection is closed. Interacting with
26+
these objects when a connection has been closed is considered undefined
27+
behaviour.
28+
829
Multithreading
930
--------------
1031

0 commit comments

Comments
 (0)