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

asyncio.timeout could be used as a decorator #100283

Open
alex-janss opened this issue Dec 15, 2022 · 4 comments · May be fixed by #100284
Open

asyncio.timeout could be used as a decorator #100283

alex-janss opened this issue Dec 15, 2022 · 4 comments · May be fixed by #100284
Labels
expert-asyncio pending The issue will be closed if no feedback is provided type-feature A feature request or enhancement

Comments

@alex-janss
Copy link

alex-janss commented Dec 15, 2022

Feature or enhancement

Allow asyncio.timeout and asyncio.timeout_at to also be used as decorators.

Pitch

It would be convenient to be able to wrap coroutines with timeouts:

@asyncio.timeout(3)
async def foo():
    await asyncio.sleep(10)

This reduces nesting and makes the timeout more visible, an argument made here: contextlib.ContextDecorator. Note that this functionality is included by default for any context managers created with contextlib, and could be easily implemented by subclassing contextlib.AsyncContextDecorator.

@alex-janss alex-janss added the type-feature A feature request or enhancement label Dec 15, 2022
@alex-janss alex-janss linked a pull request Dec 15, 2022 that will close this issue
@gvanrossum
Copy link
Member

gvanrossum commented Dec 15, 2022

Hm. TOOWTDI.

@alex-janss
Copy link
Author

alex-janss commented Dec 16, 2022

Hm. TOOWTDI.

True, but contextlib already sets the precedent for having ContextDecorators, and the documentation there argues for the decorator functionality being preferred in certain use cases. This seems especially true in asyncio.timeout in cases where we simply want to wrap an entire coroutine in a timeout without doing any rescheduling.

@gvanrossum
Copy link
Member

gvanrossum commented Dec 16, 2022

It’s a minor convenience at best, and we don’t have enough experience with asyncio.timeout to know whether it is needed often.

I personally find contextlib rather baroque, an prefer a sparser design. But I am repeating myself.

@kumaraditya303
Copy link
Contributor

kumaraditya303 commented Dec 18, 2022

I have been using https://github.com/aio-libs/async-timeout for a long time before it was added to stdlib and never needed this. It is much better to use it as a context manager and it makes the timeout visible at call site rather than having to look at the function for its timeout. If you need this, you can create a helper decorator but it doesn't need to be in the stdlib.

@kumaraditya303 kumaraditya303 added the pending The issue will be closed if no feedback is provided label Dec 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
expert-asyncio pending The issue will be closed if no feedback is provided type-feature A feature request or enhancement
Projects
Status: Todo
Development

Successfully merging a pull request may close this issue.

4 participants