Are grequests responses in the same order as the requests?

你。 提交于 2019-12-12 16:29:04

问题


I am using the grequests to asynchronously download data from a website using the same url but different parameters.

For example,

unsent_requests = []
for param in params: # assume params is a list containing different parameters or query strings
    unsent_requests.append(grequests.get(url = url, params = param))

responses = grequests.map(unsent)

How can I possibly get to know which response from responses belongs to which request from unsent_requests? Or are the responses in the same order as the unsent requests?

PS: response.url does not give any clue because a completely different url returns.


回答1:


Responses are in the same order as the requests, as shown in the usage example:

>>> reqs = [
...    grequests.get('http://httpbin.org/delay/1', timeout=0.001),
...    grequests.get('http://fakedomain/'),
...    grequests.get('http://httpbin.org/status/500')]
>>> grequests.map(reqs, exception_handler=exception_handler)
Request failed
Request failed
[None, None, <Response [500]>]



回答2:


No, they are not, the very nature of asynchronous requests means that despite being processed in order, the actual responses are not guaranteed to be in order.

This is why the response contains the originalrequest, which you can see by iterating through the result of map and printing response.request.url.



来源:https://stackoverflow.com/questions/37502384/are-grequests-responses-in-the-same-order-as-the-requests

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