[Python] Using a dict for caching async function result does not use cached results

Discussão em 'Python' iniciado por Stack, Fevereiro 5, 2025.

  1. Stack

    Stack Membro Participativo

    BASE_URL = "https://query2.finance.yahoo.com/"
    DATA_URL_PART = "v8/finance/chart/{ticker}"

    async def fetch_close_price(aiosession: aiohttp.ClientSession,
    ticker: str, start: datetime, end: datetime, ticker_price_dict):
    params = {
    'period1': int(to_midnight(start, naive=False).timestamp()),
    'period2': int(to_midnight(end, naive=False).timestamp()),
    'interval': '1d',
    'includeAdjustedClose': 'false' # Explicitly exclude adjusted close prices
    }

    print(f'Checking cache for ticker {ticker}')
    if ticker in ticker_price_dict:
    print(f'Found cache for ticker {ticker}')
    return ticker_price_dict[ticker]

    async with aiosession.get(DATA_URL_PART.format(ticker=ticker), params=params) as response:
    if response.status == 200:
    print(f"Downloading {ticker}", response.status)
    data = await response.json()
    ....
    ....
    df = pd.DataFrame({ticker: close_prices},
    index=dates)
    ticker_price_dict[ticker] = df
    print(f"Downloaded and setting {ticker}", response.status)
    return df
    else:
    print(f"Failed to fetch data for {ticker}", response.status)
    return pd.DataFrame()

    async def download_close_prices(aiosession: aiohttp.ClientSession,
    tickers: list[str], start: datetime, end: datetime):
    all_ticker_close = pd.DataFrame()
    ticker_price_dict = {}
    tasks = [fetch_close_price(aiosession, ticker, start, end, ticker_price_dict) for ticker in tickers]
    results = await asyncio.gather(*tasks)

    async def download_close_prices_all(tickers, start, end):
    connector = aiohttp.TCPConnector(limit=50)
    async with aiohttp.ClientSession(BASE_URL, connector=connector) as aiosession:
    return await download_close_prices(aiosession, tickers, start, end)

    if __name__ == "__main__":
    tickers = ["AAPL", "USDINR=X", "XCN18679-USD", "XCN18679-USD", "XCN18679-USD"]
    close_prices = asyncio.run(download_close_prices_all(tickers, start, end))


    I have this asyncio function which uses aiohttp to make requests and return dataframes, sometimes I might have lots of repeated tickers to fetch like the example given where I am calling "XCN18679-USD" many times, I tried passing in a simple dict to cache the results but it never seems to find the ticker in dict and always downloads, I don't know what to do next, maybe use cache, but sometimes I can get download failed and I don't want to cache that. Can anyone point me in the right direction what I might be doing wrong? thanks a lot to everyone in advance.

    Continue reading...

Compartilhe esta Página