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
- 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
- Merge pull request #65 from sigmavirus24/patch-1 Kenneth Reitz 8 years ago
- Merge pull request #66 from msabramo/readme_syntax_highlight Kenneth Reitz 8 years ago
- Merge pull request #71 from eugene-eeo/master Kenneth Reitz 8 years ago
- Merge pull request #58 from rtdean/exceptions Kenneth Reitz 8 years ago
- Removed redundant whitespace Eugene Eeo 8 years ago
- Use is not instead of != Eugene Eeo 8 years ago
- README.rst: syntax highlight Python code Marc Abramowitz 9 years ago
- Do not use equality when comparing with None Ian Cordasco 9 years ago
- Make sure that a request mapped to None has an exception and wasn't caused by something such as a joinall timeout. Alexander Simeonov 9 years ago
- Added the ability to set Gevent joinall timeout! Alexander Simeonov 9 years ago