From 4d42a69c79af7e22a7eecea5468b373a960df3f7 Mon Sep 17 00:00:00 2001 From: Zhaofeng Zhang <24791380+vcfgv@users.noreply.github.com> Date: Tue, 28 Oct 2025 17:39:51 +0800 Subject: [PATCH] refactor: use run_in_threadpool for asset service calls in watchlist router --- .../valuecell/server/api/routers/watchlist.py | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/python/valuecell/server/api/routers/watchlist.py b/python/valuecell/server/api/routers/watchlist.py index 5c4679287..b7dc6d334 100644 --- a/python/valuecell/server/api/routers/watchlist.py +++ b/python/valuecell/server/api/routers/watchlist.py @@ -3,6 +3,7 @@ from typing import List, Optional from fastapi import APIRouter, HTTPException, Path, Query +from starlette.concurrency import run_in_threadpool from ....utils.i18n_utils import parse_and_validate_utc_dates from ...db.repositories.watchlist_repository import get_watchlist_repository @@ -60,7 +61,8 @@ async def search_assets( countries_list = countries.split(",") if countries else None # Perform search using asset service - result = asset_service.search_assets( + result = await run_in_threadpool( + asset_service.search_assets, query=q, asset_types=asset_types_list, exchanges=exchanges_list, @@ -106,7 +108,9 @@ async def get_asset_detail( ): """Get detailed asset information.""" try: - result = asset_service.get_asset_info(ticker, language=language) + result = await run_in_threadpool( + asset_service.get_asset_info, ticker, language=language + ) if not result.get("success", False): if "not found" in result.get("error", "").lower(): @@ -145,7 +149,9 @@ async def get_asset_price( ): """Get current asset price.""" try: - result = asset_service.get_asset_price(ticker, language=language) + result = await run_in_threadpool( + asset_service.get_asset_price, ticker, language=language + ) if not result.get("success", False): if "not available" in result.get("error", "").lower(): @@ -225,7 +231,8 @@ async def get_watchlist( """Get a specific watchlist.""" try: # Use asset service to get watchlist with prices - result = asset_service.get_watchlist( + result = await run_in_threadpool( + asset_service.get_watchlist, user_id=DEFAULT_USER_ID, watchlist_name=watchlist_name, include_prices=include_prices, @@ -517,8 +524,13 @@ async def get_asset_historical_prices( start_dt, end_dt = parse_and_validate_utc_dates(start_date, end_date) # Get historical price data - result = asset_service.get_historical_prices( - ticker, start_dt, end_dt, interval, language + result = await run_in_threadpool( + asset_service.get_historical_prices, + ticker, + start_dt, + end_dt, + interval, + language, ) if not result.get("success", False):