대시보드 분리
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
"""
|
||||
# 필요한 라이브러리 및 모듈 임포트
|
||||
from datetime import datetime, timedelta
|
||||
import asyncio
|
||||
import random
|
||||
import logging
|
||||
import pytz
|
||||
@@ -87,19 +88,21 @@ async def get_stock_prices(codes: str):
|
||||
code_list: List[str] = [c.strip() for c in codes.split(",") if c.strip()]
|
||||
prices: Dict[str, float] = {}
|
||||
|
||||
# 각 종목 코드에 대해 순차적으로 현재가 조회
|
||||
# 클래스 내부에 API 호출 간격을 조절하는 쓰로틀링 기능이 내장되어 있음
|
||||
for code in code_list:
|
||||
# 병렬로 현재가 일괄 조회 (asyncio.gather)
|
||||
async def _fetch_one(code: str):
|
||||
try:
|
||||
data = await korea_investment_api.get_current_price(code)
|
||||
if data and data.get("output"):
|
||||
o = data["output"]
|
||||
# 콤마가 포함된 가격 문자열을 float으로 변환
|
||||
current_price = float(str(o.get("stck_prpr", "0")).replace(",", ""))
|
||||
prices[code] = current_price
|
||||
return code, float(str(o.get("stck_prpr", "0")).replace(",", ""))
|
||||
except Exception as e:
|
||||
# 특정 종목 조회 실패 시 경고 로그 기록
|
||||
logger.warning(f"Failed to fetch price for {code}: {e}")
|
||||
return code, None
|
||||
|
||||
results = await asyncio.gather(*[_fetch_one(c) for c in code_list])
|
||||
for code, price in results:
|
||||
if price is not None:
|
||||
prices[code] = price
|
||||
|
||||
# 조회 결과를 딕셔너리 형태로 반환
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user