Coder Social home page Coder Social logo

Asyncio integration about click HOT 8 CLOSED

tailhook avatar tailhook commented on May 7, 2024 5
Asyncio integration

from click.

Comments (8)

jodal avatar jodal commented on May 7, 2024 28

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.

mitsuhiko avatar mitsuhiko commented on May 7, 2024 23

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.

jamesstidard avatar jamesstidard commented on May 7, 2024 16

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.

rabernat avatar rabernat commented on May 7, 2024 10

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.

Tails avatar Tails commented on May 7, 2024 8

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.

Any update on this? Is it possible to pass an async generator into a progressbar as iterable for example?

from click.

vartagg avatar vartagg commented on May 7, 2024 3

BTW, there is the fork of click, which allows to use it with asyncio https://github.com/python-trio/trio-click

from click.

tailhook avatar tailhook commented on May 7, 2024

Ah, Thanks. I don't know why I didn't find this solution before :)

from click.

amitkot avatar amitkot commented on May 7, 2024

Can this issue be re-opened and considered?

from click.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.