Skip to main content

Asynchronous Python HTTP for Humans.

Project description

Requests-Futures: Asynchronous Python HTTP Requests for Humans

Small add-on for the requests http library. Makes use of python 3.3’s concurrent.futures or the backport for prior versions of python.

The additional API is minimal and obeys the standard behavior of futures. The synchronous code of two request show below.

from requests import Session

session = Session()
# first requests starts and blocks until finished
response_one = session.get('')
# second request starts once first is finished
response_two = session.get('')
# both requests are complete
print('response one status: {0}'.format(response_one.status_code))
print('response two status: {0}'.format(response_two.status_code))

The above requests can be made asynchronous by making the following modifications.

from requests_futures.sessions import FuturesSession

session = FuturesSession()
# first request is started in background
future_one = session.get('')
# second requests is started immediately
future_two = session.get('')
# wait for the first request to complete, if it hasn't already
response_one = future_one.result()
print('response one status: {0}'.format(response_one.status_code))
# wait for the second request to complete, if it hasn't already
response_two = future_two.result()
print('response two status: {0}'.format(response_two.status_code))

By default a ThreadPoolExecutor is created with 2 workers. If you would like to adjust that value or share a executor across multiple sessions you can provide one to the FuturesSession constructor.

from concurrent.futures import ThreadPoolExecutor
from requests_futures.sessions import FuturesSession

session = FuturesSession(executor=ThreadPoolExecutor(max_workers=10))
# ...

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 moved there.

Working in the Background

There is one additional parameter to the various request function, background_callback, which allows you to work with the Response objects in the background thread. This can be useful for shifting work out of the foreground, for a simple example take json parsing.

from pprint import pprint
from requests_futures.sessions import FuturesSession

session = FuturesSession()

def bg_cb(sess, resp):
    # parse the json storing the result on the response object = resp.json()

future = session.get('', background_callback=bg_cb)
# do some other stuff, send some more requests while this one works
response = future.result()
print('response status {0}'.format(response.status_code)
# data will have been attached to the response object in the background


Python 3.3:

pip install requests requests-futures

Prior versions:

pip install futures requests requests-futures

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for requests-futures, version 0.9.0
Filename, size File type Python version Upload date Hashes
Filename, size requests-futures-0.9.0.tar.gz (3.8 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page