Giter VIP home page Giter VIP logo

gevent_requests's Introduction

gevent-requests: Asynchronous Requests


PyPI - License version pyversions PyPI - Downloads

gevent-requests allows you to use Requests with Gevent to make asynchronous HTTP Requests easily.

A fork from grequests <https://github.com/spyoungtech/grequests>.

It is highly recommended that you patch from the entry and do not include thread, this package does not include patch!

pip install gevent-requests

Usage is simple:

from gevent import monkey
monkey.patch_all(thread=False, select=False)
import gevent_requests

urls = [
    'http://www.heroku.com',
    'http://python-tablib.org',
    'http://httpbin.org',
    'https://shields.io',
    'http://fakedomain/',
]

Create a set of unsent Requests:

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

Send them all at the same time:

>>> print(gevent_requests.gmap(rs))
[<Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [502]>]

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 = [
...    gevent_requests.get('http://httpbin.org/delay/1', timeout=0.001),
...    gevent_requests.get('http://fakedomain/'),
...    gevent_requests.get('http://httpbin.org/status/500')]
>>> print(gevent_requests.gmap(reqs, exception_handler=exception_handler))
Request failed
[None, <Response [502]>, <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.

Use indexed requests in gevent, use gimap_enumerate will yield a indexed requests just like enumerate return, an index and a response:

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

>>> for i in gevent_requests.gimap_enumerate(rs, size=len(urls)):
        print(i)
(4, <Response [502]>)
(2, <Response [200]>)
(0, <Response [200]>)
(3, <Response [200]>)
(1, <Response [200]>)

gevent_requests's People

Contributors

belingud avatar

Stargazers

nick avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.