From 7742eae2becc853df07f639075a520c629c98c2e Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Sun, 23 Feb 2025 11:50:33 +0100 Subject: [PATCH] optimize backend --- app/main.py | 28 +++++++++++++++++++--------- requirements.txt | 3 ++- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/app/main.py b/app/main.py index b415c7d..3c26f85 100755 --- a/app/main.py +++ b/app/main.py @@ -62,6 +62,8 @@ PRIORITY_STRATEGIES = { 'name_contains': 5 } +client = httpx.AsyncClient(http2=True, timeout=10.0) + def calculate_score(item: Dict, search_query: str) -> int: name_lower = item['name'].lower() symbol_lower = item['symbol'].lower() @@ -4269,24 +4271,29 @@ async def get_data(data:TickerData, api_key: str = Security(get_api_key)): async def fetch_data(client, endpoint, ticker): url = f"{API_URL}{endpoint}" try: - response = await client.post(url, json={"ticker": ticker}, headers={"X-API-KEY": STOCKNEAR_API_KEY}) + response = await client.post( + url, + json={"ticker": ticker}, + headers={"X-API-KEY": STOCKNEAR_API_KEY} + ) response.raise_for_status() + # Parse the JSON response return {endpoint: response.json()} except Exception as e: return {endpoint: {"error": str(e)}} @app.post("/bulk-data") -async def get_stock_data(data:BulkList, api_key: str = Security(get_api_key)): +async def get_stock_data(data: BulkList, api_key: str = Security(get_api_key)): endpoints = data.endpoints ticker = data.ticker.upper() - - async with httpx.AsyncClient() as client: - tasks = [fetch_data(client, endpoint, ticker) for endpoint in endpoints] - results = await asyncio.gather(*tasks) - # Combine results - data = {k: v for result in results for k, v in result.items()} - return data + # Create tasks for each endpoint concurrently. + tasks = [fetch_data(client, endpoint, ticker) for endpoint in endpoints] + results = await asyncio.gather(*tasks) + + # Combine the results into a single dictionary. + combined_data = {k: v for result in results for k, v in result.items()} + return combined_data @app.get("/newsletter") @@ -4298,3 +4305,6 @@ async def get_newsletter(): res = [] return res +@app.on_event("shutdown") +async def shutdown_event(): + await client.aclose() diff --git a/requirements.txt b/requirements.txt index e847323..5435fab 100755 --- a/requirements.txt +++ b/requirements.txt @@ -21,6 +21,7 @@ redis[hiredis] asyncio aiohttp httpx +httpx[http2] prophet schedule pocketbase @@ -38,4 +39,4 @@ praw fuzzywuzzy python-Levenshtein plotly==5.23.0 -kaleido==0.2.1 \ No newline at end of file +kaleido==0.2.1