-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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 integration #85
Comments
Why does that have to be in click? Just have a custom decorator that does import asyncio
from functools import update_wrapper
def coro(f):
f = asyncio.coroutine(f)
def wrapper(*args, **kwargs):
loop = asyncio.get_event_loop()
return loop.run_until_complete(f(*args, **kwargs))
return update_wrapper(wrapper, f) Would be used like this then: @click.command()
@coro
def command():
yield from whatever For more complex cases you need to accept the loop anyways or get it from somewhere. Generally I don't want to put builtin support for asyncio into click. It really does not belong there and by pure user count someone will come and ask for twisted or whatever support next :) |
Ah, Thanks. I don't know why I didn't find this solution before :) |
Just curious if this is still the recommended practice now that async/await is a first-class language feature. I feel a little dirty having to redundantly decorate all my commands with a Thanks - minor thing I know. |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This is an updated version making use of import asyncio
from functools import wraps
def coro(f):
@wraps(f)
def wrapper(*args, **kwargs):
return asyncio.run(f(*args, **kwargs))
return wrapper Usage: @click.command()
@coro
async def command():
await asyncio.sleep(1)
click.echo("Delayed hello") |
BTW, there is the fork of click, which allows to use it with asyncio https://github.com/python-trio/trio-click |
New click user here. Thanks for this great tool. I naively tried decorating a coroutine with
I believe it would be reasonable for |
It would be nice if I could do:
I think the only place that should be changed to make that work would be
Context.invoke
(check if function is coroutine, doloop.run_until_complete()
). But it's hard to subclass context in current click.Would you mind making this functionality built-in?
The text was updated successfully, but these errors were encountered: