问题
So the idea is to collect responses for 1 million queries and store them in a dictionary. 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. 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?
回答1:
The requests-async
pacakge provides asyncio support for requests
... https://github.com/encode/requests-async
Either that or use aiohttp
.
来源:https://stackoverflow.com/questions/53318354/asynchronous-python-requests-post