Replies: 1 comment 7 replies
-
|
Because when you stream a response body you need to make sure that you close the response. Handling that within a context manager is more appropriate to ensure that always occurs properly. To put it into another context, it's similar to us only allowing this kind of file opening API... # Recommended way to open files in Python,
# since it ensures you can't accidentally fail to close it,
# and the context *shows* the scope within which the resource is used.
open("some_file.txt", "r") as some_file:
...And not allowing this kind of usage... # Bad practice. Using a context manager is a better construct.
some_file = open("some_file.txt", "r")
# Oops, I better call `some_file.close()` somewhere.
# And I better make sure it's always called even if I get some unexpected exception along the way.
# I guess I could do that with a `try...finally...` block. Wait a minute why didn't I just use a context manager? |
Beta Was this translation helpful? Give feedback.
7 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Examples:
the requests uses https://docs.python-requests.org/en/latest/user/quickstart/#raw-response-content.
the aiohttp doesn't reads the HTTP content until
await.Since httpx is async-able, could httpx not to read the content(probably a doze MB response) until
await, or mergeclient.streamto ordinaryclient.get/postfor convenient like the requests?Beta Was this translation helpful? Give feedback.
All reactions