Asynchronous python requests.post()

♀尐吖头ヾ 提交于 2020-04-14 06:19:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!