diff options
author | Jordan Cook <jordan.cook@pioneer.com> | 2021-08-27 18:38:21 -0500 |
---|---|---|
committer | Jordan Cook <jordan.cook@pioneer.com> | 2021-08-27 18:58:27 -0500 |
commit | 7bfa27e6987198be77acafb91d573f9a0acddf9f (patch) | |
tree | 2d95bd68176cf2d48335191f5cbfda7fde7623e2 /examples | |
parent | 364f3653ba92620090ba9fdbf73135944fbbe45b (diff) | |
download | requests-cache-7bfa27e6987198be77acafb91d573f9a0acddf9f.tar.gz |
Add example with ThreadPoolExecutor
Diffstat (limited to 'examples')
-rwxr-xr-x | examples/basic_usage.py | 2 | ||||
-rwxr-xr-x[-rw-r--r--] | examples/custom_request_matcher.py | 0 | ||||
-rwxr-xr-x | examples/threads.py | 39 |
3 files changed, 40 insertions, 1 deletions
diff --git a/examples/basic_usage.py b/examples/basic_usage.py index 21651dc..3f0d82d 100755 --- a/examples/basic_usage.py +++ b/examples/basic_usage.py @@ -25,7 +25,7 @@ def main(): # Get some debugging info about the cache print(session.cache) - print('Cached URLS:', session.cache.urls) + print('Cached URLS:', list(session.cache.urls)) if __name__ == "__main__": diff --git a/examples/custom_request_matcher.py b/examples/custom_request_matcher.py index a63139b..a63139b 100644..100755 --- a/examples/custom_request_matcher.py +++ b/examples/custom_request_matcher.py diff --git a/examples/threads.py b/examples/threads.py new file mode 100755 index 0000000..7a07526 --- /dev/null +++ b/examples/threads.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python +""" +An example of making multi-threaded cached requests, adapted from the python docs for +{py:class}`~concurrent.futures.ThreadPoolExecutor`. +""" +from concurrent.futures import ThreadPoolExecutor, as_completed +from time import perf_counter as time + +from requests_cache import CachedSession + +URLS = [ + 'https://en.wikipedia.org/wiki/Python_(programming_language)', + 'https://en.wikipedia.org/wiki/Requests_(software)', + 'https://en.wikipedia.org/wiki/Cache_(computing)', + 'https://en.wikipedia.org/wiki/SQLite', + 'https://en.wikipedia.org/wiki/Redis', + 'https://en.wikipedia.org/wiki/MongoDB', +] + + +def send_requests(): + session = CachedSession('example_cache') + start = time() + + with ThreadPoolExecutor(max_workers=5) as executor: + future_to_url = {executor.submit(session.get, url): url for url in URLS} + + for future in as_completed(future_to_url): + url = future_to_url[future] + response = future.result() + from_cache = 'hit' if response.from_cache else 'miss' + print(f'{url} is {len(response.content)} bytes (cache {from_cache})') + + print(f'Elapsed: {time() - start:.3f} seconds') + + +if __name__ == '__main__': + send_requests() + send_requests() |