aboutsummaryrefslogtreecommitdiff
path: root/requests/async.py
diff options
context:
space:
mode:
Diffstat (limited to 'requests/async.py')
-rw-r--r--requests/async.py37
1 files changed, 18 insertions, 19 deletions
diff --git a/requests/async.py b/requests/async.py
index 8bafb1e..9488447 100644
--- a/requests/async.py
+++ b/requests/async.py
@@ -36,20 +36,25 @@ def patched(f):
kwargs['return_response'] = False
kwargs['prefetch'] = True
+ config = kwargs.get('config', {})
+ config.update(safe_mode=True)
+
+ kwargs['config'] = config
+
return f(*args, **kwargs)
return wrapped
-def send(r, pools=None):
- """Sends a given Request object."""
+def send(r, pool=None):
+ """Sends the request object using the specified pool. If a pool isn't
+ specified this method blocks. Pools are useful because you can specify size
+ and can hence limit concurrency."""
- if pools:
- r._pools = pools
+ if pool != None:
+ return pool.spawn(r.send)
- r.send()
-
- return r.response
+ return gevent.spawn(r.send)
# Patched requests.api functions.
@@ -71,19 +76,13 @@ def map(requests, prefetch=True, size=None):
:param size: Specifies the number of requests to make at a time. If None, no throttling occurs.
"""
- if size:
- pool = Pool(size)
- pool.map(send, requests)
- pool.join()
- else:
- jobs = [gevent.spawn(send, r) for r in requests]
- gevent.joinall(jobs)
+ requests = list(requests)
+
+ pool = Pool(size) if size else None
+ jobs = [send(r, pool) for r in requests]
+ gevent.joinall(jobs)
if prefetch:
[r.response.content for r in requests]
- return [r.response for r in requests]
-
-
-
-
+ return [r.response for r in requests] \ No newline at end of file