mirror of
https://github.com/kennethreitz/requests.git
synced 2026-06-05 22:50:18 +00:00
POOL PARTY!!!!!!!!!!!!!!!
This commit is contained in:
@@ -47,5 +47,6 @@ defaults['timeout'] = None
|
||||
defaults['max_redirects'] = 30
|
||||
defaults['decode_unicode'] = True
|
||||
defaults['keepalive'] = True
|
||||
defaults['max_connections'] = 10
|
||||
|
||||
|
||||
|
||||
+3
-3
@@ -24,7 +24,7 @@ __all__ = ('request', 'get', 'head', 'post', 'patch', 'put', 'delete')
|
||||
def request(method, url,
|
||||
params=None, data=None, headers=None, cookies=None, files=None, auth=None,
|
||||
timeout=None, allow_redirects=False, proxies=None, hooks=None,
|
||||
config=None, _connection=None):
|
||||
config=None, _pools=None):
|
||||
|
||||
"""Constructs and sends a :class:`Request <Request>`.
|
||||
Returns :class:`Response <Response>` object.
|
||||
@@ -40,7 +40,7 @@ def request(method, url,
|
||||
:param timeout: (optional) Float describing the timeout of the request.
|
||||
:param allow_redirects: (optional) Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.
|
||||
:param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
|
||||
:param _connection: (optional) An HTTP Connection to re-use.
|
||||
:param _pools: (optional) An HTTP PoolManager to use.
|
||||
"""
|
||||
|
||||
method = str(method).upper()
|
||||
@@ -80,7 +80,7 @@ def request(method, url,
|
||||
r = dispatch_hook('pre_request', hooks, r)
|
||||
|
||||
# Send the HTTP Request.
|
||||
r.send(connection=_connection)
|
||||
r.send(pools=_pools)
|
||||
|
||||
# Post-request hook.
|
||||
r = dispatch_hook('post_request', hooks, r)
|
||||
|
||||
+3
-2
@@ -227,7 +227,7 @@ class Request(object):
|
||||
|
||||
|
||||
|
||||
def send(self, connection=None, anyway=False):
|
||||
def send(self, pools=None, anyway=False):
|
||||
"""Sends the shit."""
|
||||
|
||||
# Safety check.
|
||||
@@ -254,12 +254,13 @@ class Request(object):
|
||||
|
||||
try:
|
||||
# Create a new HTTP connection, since one wasn't passed in.
|
||||
if not connection:
|
||||
if not pools:
|
||||
connection = urllib3.connection_from_url(url, timeout=self.timeout)
|
||||
|
||||
# One-off request. Delay fetching the content until needed.
|
||||
do_block = False
|
||||
else:
|
||||
connection = pools.connection_from_url(url, timeout=self.timeout)
|
||||
# Part of a connection pool, so no fancy stuff. Sorry!
|
||||
do_block = True
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ import cookielib
|
||||
from . import api
|
||||
from ._config import get_config
|
||||
from .utils import add_dict_to_cookiejar
|
||||
from .packages.urllib3.poolmanager import PoolManager
|
||||
|
||||
|
||||
def merge_kwargs(local_kwarg, default_kwarg):
|
||||
@@ -64,6 +65,10 @@ class Session(object):
|
||||
self.hooks = hooks
|
||||
self.config = get_config(config)
|
||||
|
||||
self.__pool = PoolManager(
|
||||
num_pools=self.config.get('max_connections')
|
||||
)
|
||||
|
||||
# Map and wrap requests.api methods.
|
||||
self._map_api_methods()
|
||||
|
||||
@@ -105,6 +110,10 @@ class Session(object):
|
||||
if k not in _kwargs:
|
||||
_kwargs[k] = v
|
||||
|
||||
# Add in PoolManager, if neccesary.
|
||||
if self.config.get('keepalive'):
|
||||
_kwargs['_pool'] = self.__pool
|
||||
|
||||
# TODO: Persist cookies.
|
||||
|
||||
return func(*args, **_kwargs)
|
||||
|
||||
Reference in New Issue
Block a user