GRequests: Asynchronous Requests
GRequests allows you to use Requests with Gevent to make asynchronous HTTP Requests easily.
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 ✨🍰✨
Commit History @3c985c6f-9137-4823-a8de-be5ec917969c/upstream
- Import upstream version 0.6.0 Kali Janitor 3 years ago
- version 0.6.0 :package: Spencer Phillip Young (commit: GitHub) 4 years ago
- Merge pull request #139 from spyoungtech/add-manifest-file Spencer Phillip Young (commit: GitHub) 4 years ago
- close session if it won't be reused (#138) Spencer Phillip Young (commit: GitHub) 4 years ago
- include license in manifest Spencer Phillip Young (commit: GitHub) 4 years ago
- update readme examples to Python 3.x Spencer Phillip Young (commit: GitHub) 4 years ago
- update authors from git log Spencer Phillip Young (commit: GitHub) 4 years ago
- version 0.5.0 :package: Spencer Phillip Young (commit: GitHub) 4 years ago
- call exception handler even if there's no exception attribute (#109) 崔庆才丨静觅 (commit: GitHub) 4 years ago
- New upstream version 0.4.0 Sophie Brun 4 years ago
- add version badges Spencer Phillip Young (commit: GitHub) 4 years ago
- bump version Spencer Phillip Young (commit: GitHub) 4 years ago
- add travis ci config (#133) Spencer Phillip Young (commit: GitHub) 4 years ago
- Merge pull request #130 from xandrade/patch-1 Frost Ming (commit: GitHub) 4 years ago
- Minor typo Antonio A (commit: GitHub) 4 years ago
- Merge pull request #122 from jogo/classifier Kenneth Reitz (commit: GitHub) 5 years ago
- Merge pull request #126 from spyoungtech/master Spencer Young (commit: GitHub) 6 years ago
- Update AUTHORS.rst Spencer Young (commit: GitHub) 6 years ago
- imap yields from handler Spencer Young 6 years ago
- Add python version classifiers Joe Gordon 6 years ago
- Update README.rst Kenneth Reitz (commit: GitHub) 6 years ago
- Merge pull request #118 from mirceaulinic/patch-1 Kenneth Reitz (commit: GitHub) 6 years ago
- Installation section emergency hotfix Mircea Ulinic (commit: GitHub) 6 years ago
- Merge pull request #85 from AkshatM/patch-1 Kenneth Reitz 8 years ago
- Include explanation of grequests.imap() in README Akshat Mahajan 8 years ago
- Merge pull request #83 from yprez/remove-broken-test Kenneth Reitz 8 years ago
- Remove failing test Yuri Prezument 8 years ago
- Merge pull request #47 from koobs/patch-1 Kenneth Reitz 8 years ago
- Merge pull request #57 from natecode/master Kenneth Reitz 8 years ago
- Merge pull request #62 from agsimeonov/master Kenneth Reitz 8 years ago