工作中有这样的需求,快速获取多个仓库中的 commit 记录。通过学习,使用 httpx 这个支持异步的请求库和 asyncio 异步编程库并发地运行协程任务。
代码如下:
import asyncio import httpx import time headers = {'Content-Type':'application/json','charset':'UTF-8'} url = 'https://gitee.com/api/v5/repos/src-anolis-os/systemd/commits?page=1&per_page=5' async def main(url): async with httpx.AsyncClient() as client: res = await client.get(url,headers = headers) # print([res.json()[0]['commit']['message']]) try: loop = asyncio.get_event_loop() start = time.time() tasks = [ loop.create_task(main(url)) for i in range(10) ] loop.run_until_complete(asyncio.wait(tasks)) print('spent %.2fs'%(time.time() - start)) finally: loop.close()