Skip to content
Myr

Async requests docs

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Kenneth Reitz · committed by Kenneth Reitz · parent 849934c · Browse files · 1 file+34 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+34 added−0 removed

  • other:+34 added−0 removed

1 file+34 added−0 removed

modified:docs/user/advanced.rst+34 added
41 unmodified lines
42 Certain parameters are best set at the ``request.config`` level43 (e.g.. a global proxy, user agent header).444546Asynchronous Requests47----------------------4849Requests has first-class support for non-blocking i/o requests, powered50by gevent. This allows you to send a bunch of HTTP requests at the same5152First, let's import the async module. Heads up — if you don't have53**gevent** installed, this will fail.::5455 from requests import async5657The ``async`` module has the exact same api as ``requests``, except it58doesn't send the request immediately. Instead, it returns the ``Request``59object.6061We can build a list of ``Request`` objects easily::6263 urls = [64 'http://python-requests.org',65 'http://httpbin.org',66 'http://python-guide.org',67 'http://kennethreitz.com'68 ]6970 rs = [async.get(u) for u in urls]7172Now we have a list of ``Request`` objects, ready to be sent. We could73send them one at a time with ``Request.send()``, but that would take a while.74Instead, we'll send them all at the same time with ``async.map()``::7576 >>> async.map(rs)77 [<Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>]787980Event Hooks81-----------8283Requests has a hook system that you can use to manipulate portions of
88 unmodified lines