Created
September 11, 2021 19:46
-
-
Save netanel-haber/2cb9d5520eea156b37ec13ec656d8900 to your computer and use it in GitHub Desktop.
Python Has Threads!
This file contains 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
from requests import Session | |
from time import time | |
from functools import partial | |
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor | |
import requests | |
def fetch(i, fetch_object=requests): | |
return fetch_object.get(f"https://jsonplaceholder.typicode.com/todos/{i}").json()[ | |
"id" | |
] | |
def session_fetcher(session): | |
return partial(fetch, fetch_object=session) | |
NUM_REQUESTS = 4 | |
def getRange(): | |
return range(1, NUM_REQUESTS + 1) | |
def naive(): | |
with Session() as session: | |
return list(map(session_fetcher(session), getRange())) | |
def multi_threaded(): | |
with ThreadPoolExecutor(max_workers=NUM_REQUESTS) as executor: | |
with Session() as session: | |
return list(executor.map(session_fetcher(session), getRange())) | |
def multi_processed(): | |
with ProcessPoolExecutor(max_workers=NUM_REQUESTS) as executor: | |
return list(executor.map(fetch, getRange())) | |
def execute(func, name): | |
start = time() | |
results = func() | |
print(f"{name} results:", "\n", results) | |
print(f"{name} took {time()-start}s.\n") | |
if __name__ == "__main__": | |
print({"number_of_requests": NUM_REQUESTS}) | |
execute(naive, "naive") | |
execute(multi_threaded, "multi-threaded") | |
execute(multi_processed, "multi-processed") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment