From 27ee6e0f4550f5b76adb9fbc60db94436c3e0a2f Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Thu, 13 Oct 2011 21:10:25 -0400 Subject: [PATCH] Async requests docs --- docs/user/advanced.rst | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/docs/user/advanced.rst b/docs/user/advanced.rst index ae7827c..e8c812a 100644 --- a/docs/user/advanced.rst +++ b/docs/user/advanced.rst @@ -43,6 +43,40 @@ Sessions can also be used to provide default data to the request methods:: (e.g.. a global proxy, user agent header). +Asynchronous Requests +---------------------- + +Requests has first-class support for non-blocking i/o requests, powered +by gevent. This allows you to send a bunch of HTTP requests at the same + +First, let's import the async module. Heads up — if you don't have +**gevent** installed, this will fail.:: + + from requests import async + +The ``async`` module has the exact same api as ``requests``, except it +doesn't send the request immediately. Instead, it returns the ``Request`` +object. + +We can build a list of ``Request`` objects easily:: + + urls = [ + 'http://python-requests.org', + 'http://httpbin.org', + 'http://python-guide.org', + 'http://kennethreitz.com' + ] + + rs = [async.get(u) for u in urls] + +Now we have a list of ``Request`` objects, ready to be sent. We could +send them one at a time with ``Request.send()``, but that would take a while. +Instead, we'll send them all at the same time with ``async.map()``:: + + >>> async.map(rs) + [, , , ] + + Event Hooks ----------- -- 2.34.1