异步编程是现代Python开发的重要技能。
基本概念
异步编程的核心是事件循环(event loop),它负责调度和执行协程。
import asyncio
async def hello():
print('Hello')
await asyncio.sleep(1)
print('World')
asyncio.run(hello())并发执行
async def main():
tasks = [fetch_data(f'url{i}') for i in range(5)]
results = await asyncio.gather(*tasks)
print(results)
评论 (0)
暂无评论,来发表第一条评论吧!
发表评论