Last active
May 15, 2022 21:46
-
-
Save elibroftw/fc61f40da65ad3178ea03c768c86a932 to your computer and use it in GitHub Desktop.
Concurrency and Parallel Requests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import concurrent.futures | |
def get_todo(i, optional_arg=None): | |
if i < 1: raise ValueError | |
r = requests.get(f'https://jsonplaceholder.typicode.com/todos/{i}') | |
return r.json() | |
def get_todos() -> tuple: | |
""" | |
returns: list[dict] | |
uses a threadPoolExecutor instead of asyncio | |
""" | |
todos = [] | |
errors = [] | |
# play arround with the max_workers. I used 35 for one of my scripts but | |
# 35 was overkill for this proof of concept. | |
# ProcessPoolExecutor is useful for CPU intensive tasks like compiling and processing lots of data | |
# see https://stackoverflow.com/a/64450523/7732434 for an example than can use ProcessPoolExecutor in place of ThreadPool.. | |
with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor: | |
future_to_todo = {executor.submit(get_todo, i, optional_arg=True): i for i in range(500)} | |
# Order is not guaranteed even if you use a list. Use the value part above as an index | |
for future in concurrent.futures.as_completed(future_to_todo): | |
try: | |
todos.append(future.result()) | |
except ValueError as e: | |
errors.append(e) | |
return todos |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment