Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async context manager #535

Open
denisSurkov opened this issue Apr 10, 2020 · 5 comments
Open

Async context manager #535

denisSurkov opened this issue Apr 10, 2020 · 5 comments

Comments

@denisSurkov
Copy link

@denisSurkov denisSurkov commented Apr 10, 2020

Usecase:

c = Counter("errors_count")

@c.count_exceptions()
def foo():  # counter = 1
      raise Exception()

@c.count_exceptions()
async def foo_async(): # counter still 1
      await sleep(1)
      raise Exception()

Quick search in project did nothing to me.

It would be cool if counter (and other objects) would handle this right out of the box, especially if we're talking about metrics for web world.

@brian-brazil
Copy link
Member

@brian-brazil brian-brazil commented Apr 10, 2020

How would that work, keeping in mind that we support all the way back to 2.6?

@denisSurkov
Copy link
Author

@denisSurkov denisSurkov commented Apr 11, 2020

Well, I didn't think about it. Sorry.

@LittleLampLight
Copy link

@LittleLampLight LittleLampLight commented Apr 21, 2020

@brian-brazil, what do you think about Python version check inside decorator to exclude support for non-async syntax. Seems like it's not gonna affect backwards support. And will come really handy

@brian-brazil
Copy link
Member

@brian-brazil brian-brazil commented Apr 21, 2020

I'm not sure how that'd work given that it's a syntax thing, and thus 2.6 would choke on it.

@LittleLampLight
Copy link

@LittleLampLight LittleLampLight commented Apr 21, 2020

Yup, actually i was thinking about depending on version import of async compatible syntax. But it looks too complex and such case may be implemented easier with context manager as well.

c = Counter("errors_count")

async def foo_async():
    with c.count_exceptions():
        await sleep(1)
        raise Exception()

actually if someone really need the decorator then he can implement it by himself with a few lines of code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.