Alternative to asyncio.wait?

Viewed 6155

I get this error:

D:\pythonstuff\demo.py:28: DeprecationWarning: The explicit passing of coroutine objects to asyncio.wait() is deprecated since Python 3.8, and scheduled for removal in Python 3.11. await asyncio.wait([

Waited 1 second!
Waited 5 second!
Time passed: 0hour:0min:5sec

Process finished with exit code 0

When I run the code:

import asyncio
import time

class class1():
    async def function_inside_class(self):
        await asyncio.sleep(1)
        print("Waited 1 second!")

    async def function_inside_class2(self):
        await asyncio.sleep(5)
        print("Waited 5 second!")

def tic():
    global _start_time
    _start_time = time.time()

def tac():
    t_sec = round(time.time() - _start_time)
    (t_min, t_sec) = divmod(t_sec,60)
    (t_hour,t_min) = divmod(t_min,60)
    print('Time passed: {}hour:{}min:{}sec'.format(t_hour,t_min,t_sec))

object = class1()


async def main():
    tic()
    await asyncio.wait([
        object.function_inside_class(),
        object.function_inside_class2()
        ])
    tac()

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()

Are there any good alternatives to asyncio.wait? I don't want a warning in console every time I launch my application.

Edit: I don't want to just hide the error, that's bad practice, and I'm looking for other ways to do the same or a similar thing, not another async library to restore the old functionality.

1 Answers

You can just call it this way as it recommends in the docs here

Example from the docs:

async def foo():
    return 42

task = asyncio.create_task(foo())
done, pending = await asyncio.wait({task})

So your code would become:

await asyncio.wait([
    asyncio.create_task(object.function_inside_class()),
    asyncio.create_task(object.function_inside_class2())
    ])
Related