Comments (8)
This is an updated version making use of asyncio.run()
from Python 3.7:
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")
from click.
Why does that have to be in click? Just have a custom decorator that does asyncio.coroutine
+ whatever wrapper logic necessary. Eg:
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 :)
from click.
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 @coroutine
wrapper as well as a async def
. Or maybe theirs a neater way that I've not thought of where I don't need to decorate each command.
Thanks - minor thing I know.
from click.
New click user here. Thanks for this great tool.
I naively tried decorating a coroutine with @click.command()
and received the cryptic error:
sys:1: RuntimeWarning: coroutine 'main' was never awaited
I believe it would be reasonable for @click.command()
to examine the function it is decorating and, if a coroutine is found, automatically wrap it with the @coro
decorator suggested above. In the meantime, the workaround worked great.
from click.
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
@coroutine
wrapper as well as aasync def
. Or maybe theirs a neater way that I've not thought of where I don't need to decorate each command.Thanks - minor thing I know.
Any update on this? Is it possible to pass an async generator into a progressbar as iterable for example?
from click.
BTW, there is the fork of click, which allows to use it with asyncio https://github.com/python-trio/trio-click
from click.
Ah, Thanks. I don't know why I didn't find this solution before :)
from click.
Can this issue be re-opened and considered?
from click.
Related Issues (20)
- DOC: Add chapter on how to debug a CLI with pdb HOT 2
- Allow Hiding of Range Values for IntRange Option in --help HOT 2
- Revisiting stderr flush in CLIRunner
- feature: allow substituting command_path in command help and epilog text
- Wront Usage when running a module with python -m HOT 1
- format_help_text does not format text in a group HOT 5
- Conditionally apply options to input
- Click performance is poor HOT 1
- Request to document some basic features explicitly HOT 2
- show envvar in error hint HOT 3
- Broken message about invalid argument value for template "File ... is a directory"
- Missing return value in context manager example (Managing resources)?
- zsh completion still misbehaving in the presence of `:`
- Setting color=True in runner.invoke on windows strips ansi codes
- Better i18n with gettext: use class-based API
- Broken code example in docs under Advanced Patterns
- Utility to introspect known command arguments HOT 3
- Autocorrect wrong/misspelled option with the suggested unique possibility. HOT 1
- Docs wrongly links PRs and Issues to flask
- The type hint for the filename parameter to open_file is incomplete
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from click.