Skip to content

Instantly share code, notes, and snippets.

@sakamer71
Created June 18, 2023 16:48
Show Gist options
  • Save sakamer71/003e1feeb59ffb65173cfa8eb9970c56 to your computer and use it in GitHub Desktop.
Save sakamer71/003e1feeb59ffb65173cfa8eb9970c56 to your computer and use it in GitHub Desktop.
multithreading example
import requests
from multiprocessing import cpu_count
from concurrent.futures import ThreadPoolExecutor
print(f'my cpu count is {cpu_count()}')
from datetime import datetime
gameIds = [718136, 718106, 718096, 718080, 718067, 718054, 718047, 718013, 717913, 717895, 717882]
#gameIds = [718136, 718106, 718096, 718080, 718067, 718054, 718047, 718013, 718008, 717988, 717968, 717956, 717938, 717937, 717924, 717913, 717895, 717882, 717859, 717845, 717835, 717822, 717805, 717783, 717771, 717752, 717738, 717728]
threads = 15
def fetchGameData(gameId):
url = f"https://statsapi.mlb.com/api/v1.1/game/{gameId}/feed/live"
print(f'Getting game data for game {gameId}')
result = requests.get(url)
print(result.status_code)
return result.text
start = datetime.now()
for gameId in gameIds:
results = fetchGameData(gameId)
duration = datetime.now() - start
print(f'getting the urls took {duration}')
print('Now testing the same with multithreading')
def getGameLogs(gameIds):
with ThreadPoolExecutor(max_workers=threads) as executor:
gameData = list(executor.map(fetchGameData, gameIds))
return gameData
start = datetime.now()
gameData = getGameLogs(gameIds)
duration = datetime.now() - start
print(gameData)
print(f'getting the urls with multithreading took {duration}')
# Now you can do what you want with gameData
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment