How do I run python asynchronous script with poetry tool script?

1k views Asked by At

I want to run python asynchronous script with poetry tool script. Could you please help? In pyproject.toml, I added like this.

[tool.poetry.scripts]
clear_data = "clear_data.clear_data:main"

In my python file, I wrote like this.

from anyio import run

async def main():
    pass

if __name__ == "__main__":
    run(main)

`

I have with poetry tool script. But faced this error.

RuntimeWarning: coroutine 'main' was never awaited
  main()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

2

There are 2 answers

0
nazgul On

I guess the simplest way is to use sync entrypoint, which invokes async top-level function as per Simplest async/await example possible in Python

import asyncio

async def run():
    pass

def main():
    asyncio.run(run())
0
Julia On

The following code works with my poetry tool.script.

    def main():
        asyncio.run(start())
    
    
    if __name__ == "__main__":
        main()