Skip to content
Myr

remove requests.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 a050faf · Browse files · 1 file+0 added−106 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+0 added−106 removed

  • other:+0 added−106 removed

1 file+0 added−106 removed

modified:requests/async.py-106 removed
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_george15 from gevent.pool import Pool16except ImportError:17 raise RuntimeError('Gevent is required for requests.async.')1819# Monkey-patch.20curious_george.patch_all(thread=False, select=False)2122from . import api232425__all__ = (26 'map', 'imap',27 'get', 'options', 'head', 'post', 'put', 'patch', 'delete', 'request'28)293031def patched(f):32 """Patches a given API function to not send."""3334 def wrapped(*args, **kwargs):3536 kwargs['return_response'] = False37 kwargs['prefetch'] = True3839 config = kwargs.get('config', {})40 config.update(safe_mode=True)4142 kwargs['config'] = config4344 return f(*args, **kwargs)4546 return wrapped474849def send(r, pool=None, prefetch=False):50 """Sends the request object using the specified pool. If a pool isn't51 specified this method blocks. Pools are useful because you can specify size52 and can hence limit concurrency."""5354 if pool != None:55 return pool.spawn(r.send, prefetch=prefetch)5657 return gevent.spawn(r.send, prefetch=prefetch)585960# Patched requests.api functions.61get = patched(api.get)62options = patched(api.options)63head = patched(api.head)64post = patched(api.post)65put = patched(api.put)66patch = patched(api.patch)67delete = patched(api.delete)68request = patched(api.request)697071def map(requests, prefetch=True, size=None):72 """Concurrently converts a list of Requests to Responses.7374 :param requests: a collection of Request objects.75 :param prefetch: If False, the content will not be downloaded immediately.76 :param size: Specifies the number of requests to make at a time. If None, no throttling occurs.77 """7879 requests = list(requests)8081 pool = Pool(size) if size else None82 jobs = [send(r, pool, prefetch=prefetch) for r in requests]83 gevent.joinall(jobs)8485 return [r.response for r in requests]868788def imap(requests, prefetch=True, size=2):89 """Concurrently converts a generator object of Requests to90 a generator of Responses.9192 :param requests: a generator of Request objects.93 :param prefetch: If False, the content will not be downloaded immediately.94 :param size: Specifies the number of requests to make at a time. default is 295 """9697 pool = Pool(size)9899 def send(r):100 r.send(prefetch)101 return r.response102103 for r in pool.imap_unordered(send, requests):104 yield r105106 pool.join()