1

I wrote a python code to send async requests using grequests, how to check which request's response is this ? are responses are in same order as they are sent ?

Here is my code :

import grequests
import time

start_time = time.time()

q_values = [1,2,3,4,5,6,7,8,9,10]

headers = {"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language": "en-US,en;q=0.5", "Accept-Encoding": "gzip, deflate", "Connection": "close", "Upgrade-Insecure-Requests": "1"}
rs = (grequests.get("https://www.google.com?query="+str(u) , headers=headers) for u in q_values)


rs = grequests.map(rs,size=10)

for r in rs:
 print r

I need output like this :

q_value 1 response code is <Response [200]>
q_value 2 response code is <Response [200]>
q_value 3 response code is <Response [200]>
q_value 4 response code is <Response [200]>
Pranav Kumar
  • 33
  • 1
  • 5
  • 1
    They do stay in order. First element in rs would be for query 1, second would be for query 2 and so on. Your queries are sent asynchronously, but the results stay in the order the request generator was constructed. – Anuvab Mohanty Jan 25 '19 at 06:20

2 Answers2

1

Ok i did it with grequests hooks, here is my answer :

import grequests
import time

start_time = time.time()

q_values = [1,2,3,4,5,6,7,8,9,10]

def do_something(response, *args, **kwargs):
    url = response.url
    print (url+ " : " +str(response))


headers = {"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language": "en-US,en;q=0.5", "Accept-Encoding": "gzip, deflate", "Connection": "close", "Upgrade-Insecure-Requests": "1"}
rs = (grequests.get("https://www.google.com?query="+str(u) , headers=headers, hooks = {'response' : do_something}) for u in q_values)


rs = grequests.map(rs,size=10)

any expert can confirm is it correct ?

Pranav Kumar
  • 33
  • 1
  • 5
1

In the response object, it also store the request object according the response

rs = grequests.map(rs,size=10)

for r in rs:
   print r.request
Trieu Nguyen
  • 933
  • 1
  • 11
  • 17