12

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.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Finnbar M
  • 170
  • 1
  • 6
  • 1
    Can you not just fix the usage? IIRC, the docs explain this deprecation. – Carcigenicate Feb 03 '21 at 14:42
  • **I don't want a warning in console every time I launch my application** you can always hide them (see [here](https://stackoverflow.com/questions/14463277/how-to-disable-python-warnings)) – Countour-Integral Feb 03 '21 at 14:44
  • 3
    If something is deprecated, it's usually for a good reason, and it isn't good practice to just hide errors. My application will also be not very forwards compatible if I hide this error, because it's being removed altogether in 3.11 – Finnbar M Feb 03 '21 at 14:55

1 Answers1

17

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())
    ])
cullzie
  • 2,705
  • 2
  • 16
  • 21