Python 3.5 增加了对async def and await的支持,同样的异步代码看起来干净了很多,也更易读。
import aiohttp import asyncio async def get_status(url, id): r = await aiohttp.get(url) print(r.status, id) r.close() tasks = [] for i in range(100): tasks.append(asyncio.ensure_future(get_status('https://api.github.com/events', id=i))) if __name__ == '__main__': loop = asyncio.get_event_loop() loop.run_until_complete(asyncio.wait(tasks)) loop.close()
本文介绍Python3.5中新增的async/await特性,通过示例代码展示如何使用aiohttp和asyncio库实现高效的异步HTTP请求。通过并发获取网页状态码的方式,展现了异步编程的优势。
6011

被折叠的 条评论
为什么被折叠?



