In what way is grequests asynchronous?

后端 未结 3 456
余生分开走
余生分开走 2020-12-23 02:41

I\'ve been using the python requests library for some time, and recently had a need to make a request asynchronously, meaning I would like to send off the HTTP request, have

3条回答
  •  梦毁少年i
    2020-12-23 03:07

    Create a list of requests and then send them with .imap:

    event_list = [grequests.get(url_viol_card, params={"viol": i},
                  session=session) for i in print_ev_list]
    for r in grequests.imap(event_list, size=5):
        print(r.request.url)
    
    • session is requests.Session() object (optional)
    • size=5 send 5 requests simultaneously: as soon as one of them is completed the next one is sent
    • In this example when any request (unordered) is completed its url is printed

提交回复
热议问题