Allow Responses to be used as Context Managers
See original GitHub issueRequests API allows Responses to be used and closed as a context manager.
We should allow the following use-cases:
async def run_async():
client = httpx.AsyncClient()
async with client.get("https://example.com") as resp:
resp.raise_for_status()
def run_sync():
client = httpx.Client()
with client.get("https://example.com") as resp:
resp.raise_for_status()
See: PendragonLore/anido#1
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:8 (8 by maintainers)
Top Results From Across the Web
Context Managers and Python's with Statement
In this step-by-step tutorial, you'll learn what the Python with statement is and how to use it with existing context managers.
Read more >Python Context Managers: When and Why To Use Them
Python's context manager allows us to better manage these resources by telling an object what to do when created or destroyed. with statement ......
Read more >Context Managers in Python: Using the "with" statement
Context managers are used to set up and tear down temporary contexts, establish and resolve custom settings, and acquire and release resources.
Read more >Understanding the Python with statement and context managers
Python provides an easy way to manage resources: Context Managers. The with keyword is used. When it gets evaluated it should result in...
Read more >Context Managers and the “with” Statement in Python
What's a context manager? It's a simple “protocol” (or interface) that your object needs to follow so it can be used with the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Maybe we can put a warning on the Responses garbage collector that it wasn’t closed properly, similar to files?
Okay, so…
This is relevant only for the streaming case. In other cases the response has been closed by the point it’s returned from the function.
There’s a question here to be had in that
client.get(..., stream=True)
is actually fundamentally different toclient.get(...)
because you really want to be using a context manager for the first case.Requests has exactly the same issue, and actually makes it really easy to not close streaming responses. One option that we could do here that’s a bit radical would be to diverge from the
requests
API here, even throughstream=True
is a fairly widely used case.Eg.
client.stream()
could return a context managed response. (It might have a defaultmethod='get'
that could be overidden.)One other bugbear to be wary of here is that “stream” in this context specifically means “stream the response”, not “stream the request” (which yes, we support, but that’s different), but the naming isn’t very clear about the expectation.