Search code examples
pythonpython-3.xpython-requestsgeventgrequests

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 finish and then returns the ordered data. Whereas imap returns data immediately and order 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 finish, 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 not a single request is sent.

According to the documentation map and imap have excatly the same API.

Is this the expected behavior? Should I not use hooks with imap? I am using Python 3.5.


Solution

  • As far as I know the difference between map and imap is that map waits for all requests to finish and then returns the ordered data. Whereas imap returns data immediately and order less.

    That's not really True. map does all requests immediatly and returns the result (this can take a while so that's why you probably said "waits for all requests to finish").

    However imap returns a generator and does the requests only on demand. So you have to start iterating over the generator before requests are sent. Loop over the generator to get the results:

    for single_request in r:
        # so something with "single_request"