Skip to content
Python
Branch: master
Clone or download

Latest commit

Latest commit 565144d Jul 29, 2019

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore well added AUTHORS.rst, tests.py, requirements.py May 20, 2012
.travis.yml add travis ci config (#133) Jul 28, 2019
AUTHORS.rst Update AUTHORS.rst Feb 26, 2018
LICENSE lawyer up May 10, 2012
README.rst add version badges Jul 29, 2019
grequests.py Minor typo Jul 1, 2019
requirements.txt add travis ci config (#133) Jul 28, 2019
setup.py bump version Jul 29, 2019
tests.py imap yields from handler Dec 21, 2017

README.rst

GRequests: Asynchronous Requests

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

Usage is simple:

import grequests

urls = [
    '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:

>>> rs = (grequests.get(u) for u in urls)

Send them all at the same time:

>>> grequests.map(rs)
[<Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, None, <Response [200]>]

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:

>>> 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, <Response [500]>]

For some speed/performance gains, you may also want to use imap instead of map. imap returns a generator of responses. Order of these responses does not map to the order of the requests you send out. The API for imap is equivalent to the API for map.

Installation

Installation is easy with pip:

$ pip install grequests
✨🍰✨
You can’t perform that action at this time.