Python 同时执行多个异步函数
我想同时执行多个异步函数,下面是一个示例Python 同时执行多个异步函数,python,python-3.x,asynchronous,async-await,python-asyncio,Python,Python 3.x,Asynchronous,Async Await,Python Asyncio,我想同时执行多个异步函数,下面是一个示例 import asyncio async def sayhello(): await asyncio.sleep(5) print("hello") async def saybye(): print("Bye") async def execute(): await sayhello() await saybye() asyncio.run(execute()
import asyncio
async def sayhello():
await asyncio.sleep(5)
print("hello")
async def saybye():
print("Bye")
async def execute():
await sayhello()
await saybye()
asyncio.run(execute())
比如,我想同时执行“sayhello”和“saybye”,所以首先要说再见,等5秒钟,然后说你好,而不是等5秒钟然后说“hello,bye”如果你把睡眠放在另一个函数中怎么办?这不是重点,我想找出一种方法,通过同时执行这些函数来实现它。这只是一个简单的例子,告诉你我的麻烦。
等待asyncio.gather(sayhello(),saybye())
…伙计,python一行一行地工作,如果它看到它必须睡觉,它就会这样做,然后继续前进,至少在你的情况下是这样的,虽然我对此没有那么多的知识,谢谢@deceze,这对我很有效