并行获取板块数据
This commit is contained in:
@@ -336,7 +336,7 @@ _SECTOR_MEM_TTL = 60
|
||||
|
||||
|
||||
async def fetch_sector_list(sector_type: str) -> list[dict]:
|
||||
# 1. 内存缓存(仅交易时段有效,60s 避免重复请求)
|
||||
# 1. 内存缓存
|
||||
now = time.time()
|
||||
if sector_type in _sector_cache:
|
||||
data, ts = _sector_cache[sector_type]
|
||||
@@ -345,7 +345,7 @@ async def fetch_sector_list(sector_type: str) -> list[dict]:
|
||||
|
||||
cache_key = f"sector_list:{sector_type}"
|
||||
|
||||
# 2. 非交易时段:走磁盘持久缓存,不请求 API
|
||||
# 2. 非交易时段:走磁盘持久缓存
|
||||
if not _is_trading_time():
|
||||
cached = get_cache(cache_key)
|
||||
if cached is not None:
|
||||
@@ -353,21 +353,28 @@ async def fetch_sector_list(sector_type: str) -> list[dict]:
|
||||
_sector_cache[sector_type] = (data, now)
|
||||
return data
|
||||
|
||||
# 3. 请求 API
|
||||
data = await _fetch_push2(sector_type)
|
||||
if data:
|
||||
_sector_cache[sector_type] = (data, now)
|
||||
ttl = _sector_ttl_hours()
|
||||
# 交易时段 ttl=0 → 不写磁盘(内存缓存已够)
|
||||
if ttl > 0:
|
||||
set_cache(cache_key, json.dumps(data, ensure_ascii=False), ttl_hours=ttl)
|
||||
return data
|
||||
# 3. 并发请求 push2 和 akshare,优先使用 push2
|
||||
push2_task = asyncio.create_task(_fetch_push2(sector_type))
|
||||
akshare_task = asyncio.create_task(_fetch_akshare(sector_type))
|
||||
|
||||
data = await _fetch_akshare(sector_type)
|
||||
if data:
|
||||
_sector_cache[sector_type] = (data, now)
|
||||
# akshare 数据源更新不确定,不写入磁盘缓存
|
||||
return data
|
||||
push2_data = await push2_task
|
||||
if push2_data:
|
||||
akshare_task.cancel()
|
||||
try:
|
||||
await akshare_task
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
_sector_cache[sector_type] = (push2_data, now)
|
||||
ttl = _sector_ttl_hours()
|
||||
if ttl > 0:
|
||||
set_cache(cache_key, json.dumps(push2_data, ensure_ascii=False), ttl_hours=ttl)
|
||||
return push2_data
|
||||
|
||||
# push2 失败,用 akshare
|
||||
akshare_data = await akshare_task
|
||||
if akshare_data:
|
||||
_sector_cache[sector_type] = (akshare_data, now)
|
||||
return akshare_data
|
||||
|
||||
|
||||
async def _fetch_push2(sector_type: str) -> list[dict]:
|
||||
|
||||
Reference in New Issue
Block a user