3

I would like to create a CLI command using pyproject.toml based on async Python function. Lets say I have this Python function in example/main.py:

async def main():
    print("Hello world!")

and pyproject.toml file:

[project]
name = "example"
...

[project.scripts]
example = "example.main:main"

When I try to execute example CLI command I get this error:

$ example
<coroutine object main at 0x7af706afbb80>
sys:1: RuntimeWarning: coroutine 'main' was never awaited
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

It seems that pyproject.toml only supports synchronous function targets. Is there a way to tell pyproject.toml that the target is asynchronous and it has to be awaited without messing up with code?

3
  • 1
    Isn't it possible to call your async func with an asyncio.run, then wrap that in a sync func, then use that as the entry point? Commented Oct 15, 2024 at 7:06
  • 1
    See related How do I run python asynchronous script with poetry tool script? Commented Oct 15, 2024 at 7:51
  • 1
    @GinoMempin Yes, it is possible to quite simply convert async function to sync using asyncio.run and I would accept it as an answer to this question. However, I wanted to inquire is there a simpler way, e.g. like [project.async_scripts] section in pyproject.toml? Commented Oct 16, 2024 at 3:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.