本人新手,刚刚接触 python 异步编程,尝试了下,照着 aiohttp 官网写的,

但有个疑问,感觉自己写的并不像异步,结果也没太看出来,是不是哪里不对呢?请各位大佬指点迷津!!!🙏 🙏 🙏

async def send_get_req(session, url, headers, req_data):
    async with session.get(url, headers=headers, params=req_data) as resp:
        return await resp.json()

async def send_post_req(session, url, headers, req_data):
    async with session.post(url, headers=headers, json=req_data) as resp:
        return await resp.json()


async def run_async_yaml():   
    headers = {'Content-Type': 'application/json', 'token': token}
    async with aiohttp.ClientSession(headers=headers) as session:
        for req in get_api_reqs_list():
            url = req['apiURL']
            method = req['apiMethod']
            header = req['apiHeader']
            data = req['apiData']

            if method == 'GET':
                result = await send_get_req(session,url,header,data)
            elif method == 'POST':
                result = await send_post_req(session,url,header,data)
#             print(result)# 写会返回数据
            print("[{:%M:%S.%f}] added {} to event loop".format(datetime.datetime.now(), result))

loop = asyncio.get_event_loop()
loop.run_until_complete(run_async_yaml())

打印结果内容的顺序每次都一样,没有变化,个人弱弱滴觉得像同步,而不像异步

[57:31.506042] added {'code': '0', 'message': '返回成功!',
[57:31.552927] added {'code': '0', 'message': '返回成功!',
[57:31.593839] added {'code': '0', 'message': '返回成功!',
[57:31.637690] added {'code': '0', 'message': '返回成功!',
[57:31.675590] added {'code': '0', 'message': '返回成功!',
[57:31.714512] added {'code': '0', 'message': '返回成功!',
[57:31.766372] added {'code': '0', 'message': '返回成功!',
[57:31.843144] added {'code': '0', 'message': '返回成功!',
[57:31.895061] added {'code': '0', 'message': '返回成功!',
[57:32.070539] added {'code': '0', 'message': '返回成功!',
[57:32.242079] added {'code': '0', 'message': '返回成功!',
[57:32.281002] added [{'id': 4050, 'link': 'http://10.100.
[57:32.322896] added {'code': '0', 'message': None, 'count
[57:32.367769] added {'code': '0', 'message': None, 'count
Done in 1.247847


↙↙↙阅读原文可查看相关链接,并与作者交流