Requests + Gevent = <3
GRequests allows you to use Requests with Gevent to make asynchronous HTTP Requests easily.
|version| |pyversions|
Note: You should probably use
requests-threads_ or
requests-futures_ instead.
Usage is simple:
.. code-block:: python
import grequestsurls = [ 'http://www.heroku.com', 'http://python-tablib.org', 'http://httpbin.org', 'http://python-requests.org', 'http://fakedomain/', 'http://kennethreitz.com' ]
Create a set of unsent Requests:
.. code-block:: python
>>> rs = (grequests.get(u) for u in urls)
Send them all at the same time:
.. code-block:: python
>>> grequests.map(rs) [, , , , None, ]
Optionally, in the event of a timeout or any other exception during the connection of the request, you can add an exception handler that will be called with the request and exception inside the main thread:
.. code-block:: python
>>> def exception_handler(request, exception): ... print("Request failed")>>> reqs = [ ... grequests.get('http://httpbin.org/delay/1', timeout=0.001), ... grequests.get('http://fakedomain/'), ... grequests.get('http://httpbin.org/status/500')] >>> grequests.map(reqs, exception_handler=exception_handler) Request failed Request failed [None, None, ]
For some speed/performance gains, you may also want to use
imapinstead of
map.
imapreturns a generator of responses. Order of these responses does not map to the order of the requests you send out. The API for
imapis equivalent to the API for
map.
Installation is easy with pip::
$ pip install grequests ✨🍰✨
.. |version| image:: https://img.shields.io/pypi/v/grequests.svg?colorB=blue :target: https://pypi.org/project/grequests/
.. |pyversions| image:: https://img.shields.io/pypi/pyversions/grequests.svg? :target: https://pypi.org/project/grequests/