python 异步 io 的一个小 demo

参考:https://docs.python.org/zh-cn/3/library/asyncio-task.html

工作中有这样的需求,快速获取多个仓库中的 commit 记录。通过学习,使用 httpx 这个支持异步的请求库和 asyncio 异步编程库并发地运行协程任务。
注意:gitee openapi 请求的时候最好带上 access_token。不然容易被封。

代码如下:

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()

另一种写法. [3.7之后才支持的 asyncio.run()]

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 get_commit(url):
    async with httpx.AsyncClient() as client:
        res = await client.get(url,headers = headers)
        # print([res.json()[0]['commit']['message']])


async def main():
    tasks = [
        asyncio.create_task(get_commit(url))
        for i in range(10)
    ]
    await asyncio.gather(*tasks)

asyncio.run(main())
posted @ 2022-09-14 22:41  绣幕  阅读(36)  评论(0编辑  收藏  举报