替代 asyncio.wait?
Alternative to asyncio.wait?
我收到这个错误:
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
当我运行 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()
asyncio.wait
有什么好的替代品吗?我不希望每次启动我的应用程序时都在控制台中出现警告。
编辑:我不想只是隐藏错误,这是不好的做法,我正在寻找其他方法来做相同或类似的事情,而不是另一个用于恢复旧功能的异步库。
您可以按照文档中的建议这样称呼它 here
来自文档的示例:
async def foo():
return 42
task = asyncio.create_task(foo())
done, pending = await asyncio.wait({task})
因此您的代码将变为:
await asyncio.wait([
asyncio.create_task(object.function_inside_class()),
asyncio.create_task(object.function_inside_class2())
])
我收到这个错误:
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:5secProcess finished with exit code 0
当我运行 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()
asyncio.wait
有什么好的替代品吗?我不希望每次启动我的应用程序时都在控制台中出现警告。
编辑:我不想只是隐藏错误,这是不好的做法,我正在寻找其他方法来做相同或类似的事情,而不是另一个用于恢复旧功能的异步库。
您可以按照文档中的建议这样称呼它 here
来自文档的示例:
async def foo():
return 42
task = asyncio.create_task(foo())
done, pending = await asyncio.wait({task})
因此您的代码将变为:
await asyncio.wait([
asyncio.create_task(object.function_inside_class()),
asyncio.create_task(object.function_inside_class2())
])