Python中的async/await
async/await是实现异步IO的语法糖,是Python3.7新出的关键字。async def可创建协程,而await可用来等待一个可等待对象的执行完成。这大大简化了协程的创建(在Python2中创建协程需要yield和send协同操作) 下面这个例子很简洁的说明了什么是异步IO1: import asyncio async def count(): print("One") await asyncio.sleep(1) print("Two") async def main(): await asyncio.gather(count(), count(), count()) if __name__ == "__main__": import time s = time.perf_counter() asyncio.run(main()) elapsed = time.perf_counter() - s print(f"{__file__} executed in {elapsed:0.2f} seconds.") 运行结果: $ python3 countasync.py One One One Two Two Two countasync.py executed in 1.01 seconds. gather会等待所有协程都返回后再返回一个结果列表,as_completed会当协程返回后立即返回: >>> import asyncio >>> async def coro(seq) -> list: ... """'IO' wait time is proportional to the max element....