async.py
This file
Path
requests/async.py
81 lines · 1.7 KiB · Python
Last changed by
Remove keep_alive cruft from urllib3 (for now)Kenneth Reitzbefore adoption5537902
History of this fileTake it
1# -*- coding: utf-8 -*-23"""4requests.async5~~~~~~~~~~~~~~67This 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.')1718# Monkey-patch.19curious_george.patch_all(thread=False)2021from . import api22from .hooks import dispatch_hook232425__all__ = (26 'map',27 'get', 'head', 'post', 'put', 'patch', 'delete', 'request'28)293031def _patched(f):32 """Patches a given API function to not send."""3334 def wrapped(*args, **kwargs):35 return f(*args, return_response=False, **kwargs)3637 return wrapped383940def _send(r, pools=None):41 """Sends a given Request object."""4243 if pools:44 r._pools = pools4546 r.send()4748 # Post-request hook.49 r = dispatch_hook('post_request', r.hooks, r)5051 # Response manipulation hook.52 r.response = dispatch_hook('response', r.hooks, r.response)5354 return r.response555657# Patched requests.api functions.58get = _patched(api.get)59head = _patched(api.head)60post = _patched(api.post)61put = _patched(api.put)62patch = _patched(api.patch)63delete = _patched(api.delete)64request = _patched(api.request)656667def map(requests):68 """Concurrently converts a list of Requests to Responses.6970 :param requests: a collection of Request objects.71 :param keep_alive: If True, HTTP Keep-Alive will be used.72 """7374 jobs = [gevent.spawn(_send, r) for r in requests]75 gevent.joinall(jobs)7677 return [r.response for r in requests]78798081