因此,我们的想法是收集 100 万个查询的响应并将它们存储在字典中。我希望它是异步的,因为 requests.post 每个查询需要 1 秒,并且我希望在等待响应时保持循环继续。经过一番研究,我得到了这样的东西。
async def get_response(id):
query_json = id2json_dict[id]
response = requests.post('some_url', json = query_json, verify=false)
return eval(response.text)
async def main(id_list):
for unique_id in id_list:
id2response_dict[unique_id] = get_response(unique_id)
我知道这不是异步的,我如何在其中使用“await”使其真正异步?
The requests-async
包提供 asyncio 支持requests
... https://github.com/encode/requests-async https://github.com/encode/requests-async
要么那个要么用aiohttp
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)