import asyncio
from httpx import AsyncClient
#声明该方法为异步方法对象
async def printNum(num):
$ 异步版的with as
async with AsyncClient() as client:
# 等待函数完成
req = await client.get(url="")
$ 输出结果的text
print(req.text)
if __name__ == '__main__':
# 用num作为变量遍历range(10)的结果,每次遍历都调用printNum方法
tasks = [printNum(num) for num in range(10)]
# 新建一个异步loop
loop = asyncio.get_event_loop()
# 等待异步程序完成
loop.run_until_complete(asyncio.wait(tasks))
# 关闭loop
loop.close()