Skip to content

Instantly share code, notes, and snippets.

@netanel-haber
Created September 11, 2021 19:46
Show Gist options
  • Save netanel-haber/2cb9d5520eea156b37ec13ec656d8900 to your computer and use it in GitHub Desktop.
Save netanel-haber/2cb9d5520eea156b37ec13ec656d8900 to your computer and use it in GitHub Desktop.
Python Has Threads!
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