Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asyncio coroutine never awaited error

I'm having trouble fixing and understanding the problem here. I'm using an example to learn Asyncio but the code I'm using is similar to mine but mine gives an error message saying:

sys:1: RuntimeWarning: coroutine 'run_script' was never awaited

Please any help will be greatly appreciated. Below is my code

async def run_script(script):
    print("Run", script)
    await asyncio.sleep(1)
    os.system("python " + script)

and I'm running it like this

for script in os.listdir():
    if script.endswith(".py"):
        scripts.append(run_script(script))

loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.gather(scripts))
loop.close()
like image 228
tushortz Avatar asked Feb 15 '18 11:02

tushortz


People also ask

How many times should Asyncio run () be called?

It should be used as a main entry point for asyncio programs, and should ideally only be called once. New in version 3.7.

What is Asyncio wait?

wait is asyncio. gather is called with coroutines as arguments to the function, not as an iterable of tasks. The function waits for all co-routines to complete before returning their results in a list (with the same ordering as coroutines in the argument list).

How do I use async await in Python?

The async/await keywords They simplify asynchronous programming in Python. The async keyword is used to create a Python coroutine. The await keyword suspends execution of a coroutine until it completes and returns the result data. The await keywords only works within an async function.

Is coroutine deprecated?

"@coroutine" decorator is deprecated since Python 3.8, use "async def" instead. When importing thriftpy2. contrib. aio modules with Python 3.8 or newer, a large number of deprecation warnings is trigger.


1 Answers

As @dim mentioned what's the typo in your code, you also need to be aware os.system is running in synchronous, which means scripts in your folder will be run in sequence instead of in asynchronous way.

To understand that, add file called hello_world.py:

import time
time.sleep(2)
print('hello world')

if you run you script as follows, it will cost you 2s + 2s = 4s:

loop = asyncio.get_event_loop()
loop.run_until_complete(
    asyncio.gather(
        *[run_script('hello_world.py') for _ in range(2)]
    )
)

So to solve this issue, you can use asyncio.subprocess module:

from asyncio import subprocess

async def run_script(script):
    process = await subprocess.create_subprocess_exec('python', script)
    try:
        out, err = await process.communicate()
    except Exception as err:
        print(err)

Then it will cost you only 2 sec, because it is running asynchronously.

like image 114
ZhouQuan Avatar answered Sep 21 '22 13:09

ZhouQuan