Skip to main content

A Tornado async HTTP/HTTPS client adaptor for python-requests. Forked from https://github.com/1stvamp/trequests .

Project description

https://travis-ci.org/1stvamp/trequests.png?branch=master

A Tornado async HTTP/HTTPS client adapter for python-requests.

The problem

You enjoy using Tornado to build fast non-blocking web applications, and you want to use a library from PyPI that makes a few HTTP requests, but pretty much every dev and their dog uses Requests to make HTTP requests (rightly so, because it’s awesome), but requests has no knowledge of the event loop nor can it yield when a socket blocks, which means any time you try to use a library like that it begins to block your request handling and grud-knows what other worlds of pain.

The solution

Luckily there are solutions, one such is to use the greenlet module to wrap blocking operations and swap Tornado coroutines at the right time, there is even the handy tornalet module which handles this for you.

To make life even easier, you lucky lucky people, I’ve created trequests, an async Requests adapter which uses greenlets (via tornalet) and the inbuilt non-blocking HTTP client methos in Tornado, to make any call to a library (utilizing Requests) non-blocking.

Installation

$ pip install trequests

Usage

# Assume bobs_big_data uses python-requests for HTTP requests
import bobs_big_data

from tornado.web import RequestHandler
from trequests import setup_session
from tornalet import tornalet

# Tell requests to use our AsyncHTTPadapter for the default
# session instance, you can also pass you own through
setup_session()

class WebHandler(RequestHandler):
    @tornalet
    def get(self):
        data = {'foo': 'bar'}
        # This will now unblock the current coroutine, like magic
        response = bobs_big_data.BigData(data).post()
        return self.write(response)

Tests

To run the basic testsuite hit up python setup.py test.

Caveats

trequests has been used in production in a large scale metrics application, and is a very small and quite simple module.

However I’ve released it as 0.9.x mainly because it’s missing 100% compatibility with the Requests adapter API, most noticeably cookie jar and session support, which I will improve (or please send me a pull request if you fancy adding support), and release as a 1.x branch when I have the time.

Also at the moment the setup_session utility actually monkey patches the session utility functions in Requests, as this was the only way I could see to override the mounts on “default” session instances (e.g. those created for every call when a session isn’t provided). I’m hoping to change this in the future.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

trequests_xsren-1.0.3.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

trequests_xsren-1.0.3-py2-none-any.whl (6.5 kB view details)

Uploaded Python 2

File details

Details for the file trequests_xsren-1.0.3.tar.gz.

File metadata

File hashes

Hashes for trequests_xsren-1.0.3.tar.gz
Algorithm Hash digest
SHA256 174497945fb0aa4a7aa93e30da17f0f2f46ee8645543e116dba60509a4277e6b
MD5 174e5531378aa463535ff1db5a2b2b1b
BLAKE2b-256 b0bd5dc4d54fd428e95dfcce0e317c4bb70cc2b3c222d05e609e3b26bce74311

See more details on using hashes here.

File details

Details for the file trequests_xsren-1.0.3-py2-none-any.whl.

File metadata

File hashes

Hashes for trequests_xsren-1.0.3-py2-none-any.whl
Algorithm Hash digest
SHA256 4cf679e6a938287715e1edd3adc00ade408571fcc71e407be6e04a8c3160de87
MD5 d0aa89834905560b17bf9730fc90276b
BLAKE2b-256 8f98d7ff39d7ae0ce246f626a1026e20a18f190359980ddf13bb6e59297c030d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page