• Stars
    star
    4,400
  • Rank 9,259 (Top 0.2 %)
  • Language
    Python
  • License
    BSD 2-Clause "Sim...
  • Created almost 12 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Requests + Gevent = <3

GRequests: Asynchronous Requests ===============================

GRequests allows you to use Requests with Gevent to make asynchronous HTTP Requests easily.

version pyversions

Installation

Installation is easy with pip:

$ pip install grequests
✨🍰✨

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 using map:

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

The HTTP verb methods in grequests (e.g., grequests.get, grequests.post, etc.) accept all the same keyword arguments as in the requests library.

Error Handling

To handle timeouts or any other exception during the connection of the request, you can add an optional exception handler that will be called with the request and exception inside the main thread. The value returned by your exception handler will be used in the result list returned by map.

>>> 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]>]

imap

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. You can also adjust the size argument to map or imap to increase the gevent pool size.

for resp in grequests.imap(reqs, size=10):
    print(resp)

There is also an enumerated version of imap, imap_enumerated which yields the index of the request from the original request list and its associated response. However, unlike imap, failed requests and exception handler results that return None will also be yielded (whereas in imap they are ignored). Aditionally, the requests parameter for imap_enumerated must be a sequence. Like in imap, the order in which requests are sent and received should still be considered arbitrary.

>>> rs = [grequests.get(f'https://httpbin.org/status/{code}') for code in range(200, 206)]
>>> for index, response in grequests.imap_enumerated(rs, size=5):
...     print(index, response)
1 <Response [201]>
0 <Response [200]>
4 <Response [204]>
2 <Response [202]>
5 <Response [205]>
3 <Response [203]>

gevent - when things go wrong

Because grequests leverages gevent (which in turn uses monkeypatching for enabling concurrency), you will often need to make sure grequests is imported before other libraries, especially requests, to avoid problems. See grequests gevent issues for additional information.

# GOOD
import grequests
import requests

# BAD
import requests
import grequests

More Repositories

1

ahk

Python wrapper for AutoHotkey with full type support. Harness the automation power of AutoHotkey with the beauty of Python.
Python
769
star
2

behave-webdriver

Selenium webdriver step library for use with the behave BDD testing framework
Python
58
star
3

mirror-action

A GitHub Action for mirroring your repository to a different remote repository
Shell
49
star
4

hikvision-recover

Command-line tool for generating recovery codes for Hikvision IP Cameras
Python
48
star
5

pyclip

Cross-platform Clipboard module for Python with binary support.
Python
40
star
6

json-five

Python JSON5 parser with round-trip preservation of comments
Python
22
star
7

behave-classy

Class-based step implementations for the Python behave BDD framework
Python
15
star
8

certbot-route53-hook

Cerbot manual auth hook for satisfying dns challenges via AWS Route 53 / boto3
Python
4
star
9

voice-commander

cross-platform voice-activation hooks and keyboard macros
Python
4
star
10

win-bash-aliases

Bash commands for windows
Python
2
star
11

gitlab-ci-shellcheck

Tool (and pre-commit hook) for checking shell scripts embedded in GitLab-CI configuration files
Python
2
star
12

behave-problem-example

A temporary repository to demonstrate a problem.
Python
2
star
13

gitlab-elasticsearch-indexer

Go
1
star
14

breachman

A puzzle game inspired by the Cyberpunk 2077 breaching protocol minigame, implemented in Python
Python
1
star
15

callsign-client-kivy

Python
1
star
16

ahk-rewrite

A temporary repo for ongoing rewrite for `ahk`. Eventually changes will be committed in the official repo
Python
1
star
17

callsign-data

download and parse ULS Amateur License records from FCC ULS downloads
Python
1
star
18

pyfilehash

A tool for generating checksums for files.
Python
1
star
19

chunking

tools for chunking iterables
Python
1
star
20

lights

quick scripts for controlling my phue lights from CLI
Python
1
star
21

ahk-binary

Convenience AutoHotkey binary install for Python ahk
Shell
1
star
22

selenium-utils-python

Quality of life utilities for selenium.
Python
1
star