Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Note</h2> <p>The below answer is <em>not</em> applicable to requests v0.13.0+. The asynchronous functionality was moved to <a href="https://github.com/kennethreitz/grequests" rel="noreferrer">grequests</a> after this question was written. However, you could just replace <code>requests</code> with <code>grequests</code> below and it should work.</p> <p>I've left this answer as is to reflect the original question which was about using requests &lt; v0.13.0.</p> <hr> <p>To do multiple tasks with <code>async.map</code> <em>asynchronously</em> you have to:</p> <ol> <li>Define a function for what you want to do with each object (your task)</li> <li>Add that function as an event hook in your request</li> <li>Call <code>async.map</code> on a list of all the requests / actions</li> </ol> <p>Example:</p> <pre><code>from requests import async # If using requests &gt; v0.13.0, use # from grequests import async urls = [ 'http://python-requests.org', 'http://httpbin.org', 'http://python-guide.org', 'http://kennethreitz.com' ] # A simple task to do to each response object def do_something(response): print response.url # A list to hold our things to do via async async_list = [] for u in urls: # The "hooks = {..." part is where you define what you want to do # # Note the lack of parentheses following do_something, this is # because the response will be used as the first argument automatically action_item = async.get(u, hooks = {'response' : do_something}) # Add the task to our list of things to do via async async_list.append(action_item) # Do our list of things to do via async async.map(async_list) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload