initial import of urllib3 branch async
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kenneth Reitz · committed by Kenneth Reitz · parent d47522e · Browse files · 1 file·+71 added−25 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+71 added−25 removed
- other:+71 added−25 removed
Provenance
- Commit
93c4415- Tree
a78150b- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent
d47522e
1 file changed+71 added−25 removed
modified:requests/async.py+71 added-25 removed
1 # -*- coding: utf-8 -*-2 3 """4- requests.async5- ~~~~~~~~~~~~~~4+requests.async5+~~~~~~~~~~~~~~6 7- This module implements the main Requests system, after monkey-patching8- the urllib2 module with eventlet or gevent..9-10- :copyright: (c) 2011 by Kenneth Reitz.11- :license: ISC, see LICENSE for more details.7+This module contains an asynchronous replica of ``requests.api``, powered8+by gevent. All API methods return a ``Request`` instance (as opposed to9+``Response``). A list of requests can be sent with ``map()``.10 """11 12+try:13+ import gevent14+ from gevent import monkey as curious_george15+except ImportError:16+ raise RuntimeError('Gevent is required for requests.async.')17 15-from __future__ import absolute_import18+# Monkey-patch.19+curious_george.patch_all(thread=False)20 17-import urllib18-import urllib221+from . import api22+from .hooks import dispatch_hook23+from .packages.urllib3.poolmanager import PoolManager24 20-from urllib2 import HTTPError25 26+__all__ = (27+ 'map',28+ 'get', 'head', 'post', 'put', 'patch', 'delete', 'request'29+)30 23-try:24- import eventlet25- eventlet.monkey_patch()26-except ImportError:27- pass31 29-if not 'eventlet' in locals():30- try:31- from gevent import monkey32- monkey.patch_all()33- except ImportError:34- pass32+def _patched(f):33+ """Patches a given API function to not send."""34 35+ def wrapped(*args, **kwargs):36+ return f(*args, _return_request=True, **kwargs)37 37-if not 'eventlet' in locals():38- raise ImportError('No Async adaptations of urllib2 found!')38+ return wrapped39 40 41-from .core import *41+def _send(r, pools=None):42+ """Sends a given Request object."""43+44+ if pools:45+ r._pools = pools46+47+ r.send()48+49+ # Post-request hook.50+ r = dispatch_hook('post_request', r.hooks, r)51+52+ # Response manipulation hook.53+ r.response = dispatch_hook('response', r.hooks, r.response)54+55+ return r.response56+57+58+# Patched requests.api functions.59+get = _patched(api.get)60+head = _patched(api.head)61+post = _patched(api.post)62+put = _patched(api.put)63+patch = _patched(api.patch)64+delete = _patched(api.delete)65+request = _patched(api.request)66+67+68+def map(requests, keep_alive=False):69+ """Concurrently converts a list of Requests to Responses.70+71+ :param requests: a collection of Request objects.72+ :param keep_alive: If True, HTTP Keep-Alive will be used.73+ """74+75+ if keep_alive:76+ pools = PoolManager(num_pools=len(requests), maxsize=1)77+ else:78+ pools = None79+80+ jobs = [gevent.spawn(_send, r, pools=pools) for r in requests]81+ gevent.joinall(jobs)82+83+ return [r.response for r in requests]84+85+86+87+