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
Provenance
- Commit
32327f8- Tree
5d5549b- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent
a050faf
1 file changed+0 added−106 removed
modified:requests/async.py-106 removed
1-# -*- coding: utf-8 -*-2-3-"""4-requests.async5-~~~~~~~~~~~~~~6-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- from gevent.pool import Pool16-except ImportError:17- raise RuntimeError('Gevent is required for requests.async.')18-19-# Monkey-patch.20-curious_george.patch_all(thread=False, select=False)21-22-from . import api23-24-25-__all__ = (26- 'map', 'imap',27- 'get', 'options', 'head', 'post', 'put', 'patch', 'delete', 'request'28-)29-30-31-def patched(f):32- """Patches a given API function to not send."""33-34- def wrapped(*args, **kwargs):35-36- kwargs['return_response'] = False37- kwargs['prefetch'] = True38-39- config = kwargs.get('config', {})40- config.update(safe_mode=True)41-42- kwargs['config'] = config43-44- return f(*args, **kwargs)45-46- return wrapped47-48-49-def 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."""53-54- if pool != None:55- return pool.spawn(r.send, prefetch=prefetch)56-57- return gevent.spawn(r.send, prefetch=prefetch)58-59-60-# Patched requests.api functions.61-get = patched(api.get)62-options = patched(api.options)63-head = patched(api.head)64-post = patched(api.post)65-put = patched(api.put)66-patch = patched(api.patch)67-delete = patched(api.delete)68-request = patched(api.request)69-70-71-def map(requests, prefetch=True, size=None):72- """Concurrently converts a list of Requests to Responses.73-74- :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- """78-79- requests = list(requests)80-81- pool = Pool(size) if size else None82- jobs = [send(r, pool, prefetch=prefetch) for r in requests]83- gevent.joinall(jobs)84-85- return [r.response for r in requests]86-87-88-def imap(requests, prefetch=True, size=2):89- """Concurrently converts a generator object of Requests to90- a generator of Responses.91-92- :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- """96-97- pool = Pool(size)98-99- def send(r):100- r.send(prefetch)101- return r.response102-103- for r in pool.imap_unordered(send, requests):104- yield r105-106- pool.join()