import asyncio
import threading
# 传入name参数:
async def hello(name):
# 打印name和当前线程:
print("Hello %s! (%s)" % (name, threading.current_thread))
# 异步调用asyncio.sleep(1):
await asyncio.sleep(10)
print("Hello %s again! (%s)" % (name, threading.current_thread))
return name
# syncio.ensure_future是Python asyncio库中用于安排协程或Future对象执行的函数,其核心作用是将可等待对象(如协程或Future)加入任务队列并确保其被调度执行,即使未被显式await
async def main():
L = await asyncio.gather(hello("Bob"), hello("Alice"))
#L = await asyncio.wait(hello("Bob"), hello("Alice"))
print(L)
asyncio.run(main())