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/threads.py | |
parent | 364f3653ba92620090ba9fdbf73135944fbbe45b (diff) | |
download | requests-cache-7bfa27e6987198be77acafb91d573f9a0acddf9f.tar.gz |
Add example with ThreadPoolExecutor
Diffstat (limited to 'examples/threads.py')
-rwxr-xr-x | examples/threads.py | 39 |
1 files changed, 39 insertions, 0 deletions
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() |