pythonasynchronouspython-poetrypython-anyio

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


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


Solution

  • 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())