Understanding requests versus grequests

后端 未结 2 1965
闹比i
闹比i 2021-02-08 07:14

I\'m working with a process which is basically as follows:

  1. Take some list of urls.
  2. Get a Response object from each.
  3. Create a BeautifulSoup object
相关标签:
2条回答
  • 2021-02-08 07:27

    I do not know the exact reason for the observed behavior with .map(). However, using the .imap() function with size=1 always returned a 'Response 200' for my few minutes testing. Here is the code snipet:

    rs = (grequests.get(u) for u in [BASE.format(t) for t in tickers])
    rsm_iterator = grequests.imap(rs, exception_handler=exception_handler, size=1)
    rsm_list = [r for r in rsm_iterator]
    print(rsm_list)
    

    And if you don't want to wait for all requests to finish before working on their answers, you can do this like so:

    rs = (grequests.get(u) for u in [BASE.format(t) for t in tickers])
    rsm_iterator = grequests.imap(rs, exception_handler=exception_handler, size=1)
    for r in rsm_iterator:
        print(r)
    
    0 讨论(0)
  • 2021-02-08 07:43

    You are just sending requests too fast. As grequests is an async lib, all of these requests are almost sent simultaneously. They are too many.

    You just need to limit the concurrent tasks by grequests.map(rs, size=your_choice), I have tested grequests.map(rs, size=10) and it works well.

    0 讨论(0)
提交回复
热议问题