nhl-api-py icon indicating copy to clipboard operation
nhl-api-py copied to clipboard

Async Stats Support

Open coreyjs opened this issue 1 year ago • 2 comments

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

coreyjs avatar Feb 09 '24 16:02 coreyjs

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}")

jaihon1 avatar Feb 20 '24 15:02 jaihon1

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

coreyjs avatar Feb 20 '24 21:02 coreyjs