Imap vs. map in grequests library

As far as I know, the difference between map and imap is that map waits for all requests to complete and then returns the ordered data. While imap returns data immediately and orders less.

When i use:

 urls = [...some_data...] rs = (grequests.get(u,, hooks=dict(response=callback_f)) for u in urls) r = grequests.map(rs) 

the hook is used as soon as all requests end and the callback function is called.

When i use:

 urls = [...some_data...] rs = (grequests.get(u,, hooks=dict(response=callback_f)) for u in urls) r = grequests.imap(rs) 

then no request is sent.

According to the documentation map and imap they have at their disposal the same API.

Is this the expected behavior? Shouldn't hooks be used with imap? I am using Python 3.5.

+5
source share
1 answer

As far as I know, the difference between map and imap is that map waits for all requests to complete and then returns the ordered data. While imap returns data immediately and orders less.

It is not true. map executes all requests immediately and returns the result (this may take some time, so you probably said "waiting for all requests to complete").

However, imap only requests on request. Thus, you need to iterate over the results to get them (but each iteration only performs one query):

 for single_request in r: # so something with "single_request" 
+3
source

Source: https://habr.com/ru/post/1263600/


All Articles