[英]Asynchronous python requests.post()
So the idea is to collect responses for 1 million queries and store them in a dictionary.所以这个想法是收集 100 万个查询的响应并将它们存储在字典中。 I want it to be asynchronous because requests.post takes 1 second for each query and I want to keep the loop going while it's wait for response.
我希望它是异步的,因为 requests.post 每次查询需要 1 秒,我希望在等待响应时保持循环。 After some research I have something like this.
经过一番研究,我有这样的事情。
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)
I know this is not asynchronous, how do I use "await" in it to make it truly asynchronous?我知道这不是异步的,我如何在其中使用“await”来使其真正异步?
The requests-async
pacakge provides asyncio support for requests
... https://github.com/encode/requests-async requests-async
pacakge 为requests
提供异步支持...... https://github.com/encode/requests-async
Either that or use aiohttp
.要么使用
aiohttp
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.