Commit db7b6c7a authored by Sebastian Ramacher's avatar Sebastian Ramacher

Imported Upstream version 0.9.7

parent 486e2087
include README.rst LICENSE test_requests_futures.py requirements-python-2.7.txt requirements-python-3.2.txt
include README.rst LICENSE test_requests_futures.py requirements-python-2.7.txt requirements-python-3.txt
Metadata-Version: 1.1
Name: requests-futures
Version: 0.9.5
Version: 0.9.7
Summary: Asynchronous Python HTTP for Humans.
Home-page: https://github.com/ross/requests-futures
Author: Ross McFarland
......@@ -76,6 +76,15 @@ Description: Asynchronous Python HTTP Requests for Humans
from requests_futures.sessions import FuturesSession
session = FuturesSession(max_workers=10)
FutureSession will use an existing session object if supplied:
.. code-block:: python
from requests import session
from requests_futures.sessions import FuturesSession
my_session = session()
future_session = FuturesSession(session=my_session)
That's it. The api of requests.Session is preserved without any modifications
beyond returning a Future rather than Response. As with all futures exceptions
are shifted (thrown) to the future.result() call so try/except blocks should be
......@@ -123,9 +132,9 @@ Classifier: Intended Audience :: Developers
Classifier: Natural Language :: English
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 2.6
Classifier: Programming Language :: Python :: 2.7
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.1
Classifier: Programming Language :: Python :: 3.2
Classifier: Programming Language :: Python :: 3.3
Classifier: Programming Language :: Python :: 3.4
Classifier: Programming Language :: Python :: 3.5
......@@ -68,6 +68,15 @@ As a shortcut in case of just increasing workers number you can pass
from requests_futures.sessions import FuturesSession
session = FuturesSession(max_workers=10)
FutureSession will use an existing session object if supplied:
.. code-block:: python
from requests import session
from requests_futures.sessions import FuturesSession
my_session = session()
future_session = FuturesSession(session=my_session)
That's it. The api of requests.Session is preserved without any modifications
beyond returning a Future rather than Response. As with all futures exceptions
are shifted (thrown) to the future.result() call so try/except blocks should be
......
Metadata-Version: 1.1
Name: requests-futures
Version: 0.9.5
Version: 0.9.7
Summary: Asynchronous Python HTTP for Humans.
Home-page: https://github.com/ross/requests-futures
Author: Ross McFarland
......@@ -76,6 +76,15 @@ Description: Asynchronous Python HTTP Requests for Humans
from requests_futures.sessions import FuturesSession
session = FuturesSession(max_workers=10)
FutureSession will use an existing session object if supplied:
.. code-block:: python
from requests import session
from requests_futures.sessions import FuturesSession
my_session = session()
future_session = FuturesSession(session=my_session)
That's it. The api of requests.Session is preserved without any modifications
beyond returning a Future rather than Response. As with all futures exceptions
are shifted (thrown) to the future.result() call so try/except blocks should be
......@@ -123,9 +132,9 @@ Classifier: Intended Audience :: Developers
Classifier: Natural Language :: English
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 2.6
Classifier: Programming Language :: Python :: 2.7
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.1
Classifier: Programming Language :: Python :: 3.2
Classifier: Programming Language :: Python :: 3.3
Classifier: Programming Language :: Python :: 3.4
Classifier: Programming Language :: Python :: 3.5
......@@ -2,7 +2,7 @@ LICENSE
MANIFEST.in
README.rst
requirements-python-2.7.txt
requirements-python-3.2.txt
requirements-python-3.txt
setup.py
test_requests_futures.py
requests_futures/__init__.py
......
requests>=1.2.0
futures>=2.1.3
\ No newline at end of file
futures>=2.1.3
......@@ -9,15 +9,16 @@ async requests HTTP library
"""
import logging
__title__ = 'requests-futures'
__version__ = '0.9.5'
__version__ = '0.9.7'
__build__ = 0x000000
__author__ = 'Ross McFarland'
__license__ = 'Apache 2.0'
__copyright__ = 'Copyright 2013 Ross McFarland'
# Set default logging handler to avoid "No handler found" warnings.
import logging
try: # Python 2.7+
from logging import NullHandler
except ImportError:
......
......@@ -24,9 +24,11 @@ from concurrent.futures import ThreadPoolExecutor
from requests import Session
from requests.adapters import DEFAULT_POOLSIZE, HTTPAdapter
class FuturesSession(Session):
def __init__(self, executor=None, max_workers=2, *args, **kwargs):
def __init__(self, executor=None, max_workers=2, session=None, *args,
**kwargs):
"""Creates a FuturesSession
Notes
......@@ -49,6 +51,7 @@ class FuturesSession(Session):
self.mount('http://', HTTPAdapter(**adapter_kwargs))
self.executor = executor
self.session = session
def request(self, *args, **kwargs):
"""Maintains the existing api for Session.request.
......@@ -59,7 +62,10 @@ class FuturesSession(Session):
response in the background, e.g. call resp.json() so that json parsing
happens in the background thread.
"""
func = sup = super(FuturesSession, self).request
if self.session:
func = sup = self.session.request
else:
func = sup = super(FuturesSession, self).request
background_callback = kwargs.pop('background_callback', None)
if background_callback:
......@@ -71,3 +77,9 @@ class FuturesSession(Session):
func = wrap
return self.executor.submit(func, *args, **kwargs)
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.executor.shutdown()
......@@ -46,11 +46,11 @@ setup(
'Natural Language :: English',
'License :: OSI Approved :: Apache Software License',
'Programming Language :: Python',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.1',
'Programming Language :: Python :: 3.2',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
),
)
......@@ -4,13 +4,14 @@
"""Tests for Requests."""
from concurrent.futures import Future
from requests import Response
from requests import Response, session
from os import environ
from requests_futures.sessions import FuturesSession
from unittest import TestCase, main
HTTPBIN = environ.get('HTTPBIN_URL', 'http://httpbin.org/')
def httpbin(*suffix):
"""Returns url for HTTPBIN resource."""
return HTTPBIN + '/'.join(suffix)
......@@ -53,6 +54,18 @@ class RequestsTestCase(TestCase):
resp = future.result()
self.assertEqual('boom', cm.exception.args[0])
def test_supplied_session(self):
""" Tests the `session` keyword argument. """
requests_session = session()
requests_session.headers['Foo'] = 'bar'
sess = FuturesSession(session=requests_session)
future = sess.get(httpbin('headers'))
self.assertIsInstance(future, Future)
resp = future.result()
self.assertIsInstance(resp, Response)
self.assertEqual(200, resp.status_code)
self.assertEqual(resp.json()['headers']['Foo'], 'bar')
def test_max_workers(self):
""" Tests the `max_workers` shortcut. """
from concurrent.futures import ThreadPoolExecutor
......@@ -79,6 +92,30 @@ class RequestsTestCase(TestCase):
resp = future.result()
self.assertEqual(404, resp.status_code)
def test_context(self):
class FuturesSessionTestHelper(FuturesSession):
def __init__(self, *args, **kwargs):
super(FuturesSessionTestHelper, self).__init__(*args, **kwargs)
self._exit_called = False
def __exit__(self, *args, **kwargs):
self._exit_called = True
return super(FuturesSessionTestHelper, self).__exit__(*args,
**kwargs)
passout = None
with FuturesSessionTestHelper() as sess:
passout = sess
future = sess.get(httpbin('get'))
self.assertIsInstance(future, Future)
resp = future.result()
self.assertIsInstance(resp, Response)
self.assertEqual(200, resp.status_code)
self.assertTrue(passout._exit_called)
if __name__ == '__main__':
main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment