demo_async.py

import asyncio
from io import StringIO
import httpx
import pandas as pd
from timer_functions import time_async

async def download_data(ticker):
    async with httpx.AsyncClient(headers={'user-agent': 'firefox'}) as client:
        url = f'https://query1.finance.yahoo.com/v7/finance/download/{ticker}?period1=1531526400&period2=1689292800&interval=1d&events=history&includeAdjustedClose=true'
        response = await client.request('GET', url)
        csv_string = StringIO(response.text)
        df = pd.read_csv(csv_string)
        df.to_csv(f'./dataset/{ticker}.csv', index=False)
        print(f'ticker {ticker} downloaded')

@time_async
async def main():
    results = await asyncio.gather(*(download_data(ticker) for ticker in tickers))
    return results
    
if __name__ == '__main__':
    tickers = ('TSMC34.SA', 'ASML', 'V', 'MU', 'H', 'ALYA', 'ARBB', 'ASGN', 'AUR', 'BBAI', 'BR', 'BTCM', 'CACI', 'CD', 'CDW', 'CLPS', 'CLVT', 'CNDT', 'CNXC', 'CRGE', 'CSPI', 'CTG')
    results = asyncio.run(main())
    print(results)
    print('Total Count:', len(tickers))



time_functions.py

import time

def time_sync(func):
    def wrapper(*args, **kwargs):
        start_time = time.perf_counter()
        result = func(*args, **kwarJgs) #
        end_time = time.perf_counter()
        print(f"Function {func.__name__} took {end_time - start_time} seconds to run.")
        return result
    return wrapper

def time_async(func):
    async def wrapper(*args, **kwargs):
        start_time = time.perf_counter()
        result = await func(*args, **kwargs) #
        end_time = time.perf_counter()
        print(f"Function {func.__name__} took {end_time - start_time} seconds to run.")
        return result
    return wrapper