Skip to content
Myr

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

1 file+71 added−25 removed

modified:requests/async.py+71 added-25 removed
1# -*- coding: utf-8 -*-23"""4 requests.async5 ~~~~~~~~~~~~~~4requests.async5~~~~~~~~~~~~~~67 This module implements the main Requests system, after monkey-patching8 the urllib2 module with eventlet or gevent..910 :copyright: (c) 2011 by Kenneth Reitz.11 :license: ISC, see LICENSE for more details.7This module contains an asynchronous replica of ``requests.api``, powered8by gevent. All API methods return a ``Request`` instance (as opposed to9``Response``). A list of requests can be sent with ``map()``.10"""1112try:13 import gevent14 from gevent import monkey as curious_george15except ImportError:16 raise RuntimeError('Gevent is required for requests.async.')1715from __future__ import absolute_import18# Monkey-patch.19curious_george.patch_all(thread=False)2017import urllib18import urllib221from . import api22from .hooks import dispatch_hook23from .packages.urllib3.poolmanager import PoolManager2420from urllib2 import HTTPError2526__all__ = (27 'map',28 'get', 'head', 'post', 'put', 'patch', 'delete', 'request'29)3023try:24 import eventlet25 eventlet.monkey_patch()26except ImportError:27 pass3129if not 'eventlet' in locals():30 try:31 from gevent import monkey32 monkey.patch_all()33 except ImportError:34 pass32def _patched(f):33 """Patches a given API function to not send."""3435 def wrapped(*args, **kwargs):36 return f(*args, _return_request=True, **kwargs)3737if not 'eventlet' in locals():38 raise ImportError('No Async adaptations of urllib2 found!')38 return wrapped394041from .core import *41def _send(r, pools=None):42 """Sends a given Request object."""4344 if pools:45 r._pools = pools4647 r.send()4849 # Post-request hook.50 r = dispatch_hook('post_request', r.hooks, r)5152 # Response manipulation hook.53 r.response = dispatch_hook('response', r.hooks, r.response)5455 return r.response565758# Patched requests.api functions.59get = _patched(api.get)60head = _patched(api.head)61post = _patched(api.post)62put = _patched(api.put)63patch = _patched(api.patch)64delete = _patched(api.delete)65request = _patched(api.request)666768def map(requests, keep_alive=False):69 """Concurrently converts a list of Requests to Responses.7071 :param requests: a collection of Request objects.72 :param keep_alive: If True, HTTP Keep-Alive will be used.73 """7475 if keep_alive:76 pools = PoolManager(num_pools=len(requests), maxsize=1)77 else:78 pools = None7980 jobs = [gevent.spawn(_send, r, pools=pools) for r in requests]81 gevent.joinall(jobs)8283 return [r.response for r in requests]84858687