• Stars
    star
    129
  • Rank 277,603 (Top 6 %)
  • Language
    Python
  • License
    BSD 3-Clause "New...
  • Created almost 11 years ago
  • Updated almost 5 years ago

Reviews

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

Repository Details

A Tornado async HTTP/HTTPS client adaptor for python-requests

trequests

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.

More Repositories

1

hippybot

Hipchat.com bot written in Python with a simple plugin API
Python
95
star
2

memorised

memcache memoization decorators and utils for python
Python
45
star
3

django-form-scaffold

Utility for generating dynamic Django template representations of the inbuilt form.as_p/as_ul/as_table output.
Python
24
star
4

django-dynamic-formset

Git mirror of django-dynamic-formset: http://code.google.com/p/django-dynamic-formset (updates every hour)
JavaScript
23
star
5

py-sparkblocks

Python library for generating unicode text based sparkline bargraphs from lists of numbers
Python
16
star
6

flask-straw-poll

Example straw poll Flask app
Python
7
star
7

django-limit-users

Installable Django app to limit number of enabled user registrations
Python
6
star
8

phpweekly.info

phpweekly.info homepage
HTML
6
star
9

oneversion

A git repo for my little bits and pieces which don't warrant their own project, tagged with OneVersion (a reference to my 1v/One Version Software freelance trading name).
PHP
4
star
10

node-clockwork

Asynchronous Node.js wrapper for the Mediaburst Clockwork SMS API
JavaScript
4
star
11

pyDBCLI

Framework for creating cmd.Cmd powered interactive command line database interfaces, ala psql and mysql
Python
3
star
12

causal

Open source lifestream and stats web app
JavaScript
2
star
13

markgen.py

Python library for the programmatic generation of markdown text
Python
2
star
14

baskit

Sandboxed PEAR and Phing based build system for PHP
PHP
2
star
15

angularpy

A wrapper around lxml to provide an extra intuitive python-object based accessor to existing XML documents, and creating new ones. Based on PHP5's SimpleXML.
Python
2
star
16

juju-npm-offline-registry

A Juju charm layer for npm-offline-registry
Python
2
star
17

custauth

Repository for the Apache2::AuthNetCust Perl module and mod_custauth Apache2 module
Perl
2
star
18

pycon-2012-talk

Sources for my talk at pycon uk 2012
JavaScript
2
star
19

thweddy

App to display conversations on twitter as a thread
Python
2
star
20

conn-check-talk

conn-check - the talk
JavaScript
1
star
21

projectcausal.com

Site files for Project Causal
1
star
22

spiderpenguin

Web based remote control for tuxdroid
Python
1
star
23

twasciiart

Simple python script for piping ASCII art and other such textual blobs to Twitter
Python
1
star
24

serverdensity-hb-plugin

HippyBot plugin to get device/service info and metrics from the serverdensity.com API
Python
1
star
25

ctfa

CapitalOnTap CSV to FreeAgent CSV transformer
JavaScript
1
star
26

blog.projectcausal.com

Habari templates for blog.projectcausal.com
PHP
1
star
27

brumcon9-talk

Python
1
star
28

sd-uwsgi-plugin

uWSGI serverdensity.com monitoring plugin
Python
1
star
29

gh-hooky

Better Github issues and notifications
JavaScript
1
star
30

stalkertux

Motion detecting tuxdroid app that makes tux follow objects moving past an attached webcam
Python
1
star
31

ansible-bzr

An ansible module for intelligently interacting with bazaar repositories
Python
1
star
32

py-serverdensity

Python
1
star
33

zom

SaaS devops bot - the smart zombie for your cloud
1
star
34

shortee

A Munchkin (and Munchkin-like) level/hit counter app for Ubuntu Touch
JavaScript
1
star
35

tuxtools

Handy tools and functions for hacking your tuxdroid
1
star
36

OnDeafTweers

Simple CLI & web script to display people on twitter following you AND conversing with you that you're not following back
Python
1
star