coreyjs / nhl-api-py

NHL API (2024 Updated) - For accessing most of the NHL EDGE statistical API's, scores, schedules and more
GNU General Public License v3.0
26 stars 6 forks source link

Async Stats Support #28

Open coreyjs opened 8 months ago

coreyjs commented 8 months ago

I am thinking about updating this to support async operations. If anyone has any feedback, for or against, I would love to hear it.

jaihon1 commented 8 months ago

Not really sure where you need async operations, but if it helps I worked a little with the new NHL API and used concurrency feature of python to get all landing data for batch games.

import concurrent.futures

def processGameLanding(game):
    game_id = game['id']

    try:
        getNHLGameLanding(game_id)
    except Exception as landing_error:
        print(f"Error retrieving Game Landing for game {game_id}: {landing_error}")

    print(f'Completed game: {game_id}')

def getAllLanding():
    num_threads = 100

    schedule_file_names = get_file_names('../data/nhl-v2/schedule')

    for schedule_file_name in schedule_file_names:
        print(f'Schedule File: {schedule_file_name}')

        games = readJSON(f'../data/nhl-v2/schedule/{schedule_file_name}')['games']

        # Create a ThreadPoolExecutor <---- This Part!
        with concurrent.futures.ThreadPoolExecutor(max_workers=num_threads) as executor:
            # Submit tasks to the executor and get a list of futures
            futures = [executor.submit(processGameLanding, game) for game in games]

            # Use as_completed to iterate over completed futures
            for future in concurrent.futures.as_completed(futures):
                try:
                    future.result()  # Retrieve the result or raise an exception if an error occurred
                except Exception as e:
                    print(f"An error occurred: {e}")
coreyjs commented 8 months ago

It was more out of a curiosity stand point on my end, but this is interesting. Thanks!