question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow Responses to be used as Context Managers

See original GitHub issue

Requests 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:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
sethmlarsoncommented, Oct 4, 2019

Maybe we can put a warning on the Responses garbage collector that it wasn’t closed properly, similar to files?

2reactions
tomchristiecommented, Sep 27, 2019

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 to client.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 through stream=True is a fairly widely used case.

Eg. client.stream() could return a context managed response. (It might have a default method='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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found