add snapshot
This commit is contained in:
@@ -3,7 +3,7 @@ from datetime import date, datetime
|
||||
from enum import Enum
|
||||
import json
|
||||
from typing import Any
|
||||
from strategy.trend.boot import get_collector_snapshot
|
||||
from libs.snapshot import get_collector_snapshot
|
||||
import httpx
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ def _json_value(value: Any) -> Any:
|
||||
return str(value)
|
||||
|
||||
|
||||
def collector_push(account_id: str, assets: Any, positions: Any) -> None:
|
||||
def collector_push(account_id: str, assets: Any, positions: Any, deals: Any = None) -> None:
|
||||
"""[暂停] 数据收集提交,太耗时,超过200毫秒."""
|
||||
try:
|
||||
payload = _json_value(
|
||||
@@ -42,6 +42,7 @@ def collector_push(account_id: str, assets: Any, positions: Any) -> None:
|
||||
"account_id": account_id,
|
||||
"assets": assets,
|
||||
"positions": positions,
|
||||
**({"deals": deals} if deals is not None else {}),
|
||||
}
|
||||
)
|
||||
httpx.post(COLLECTOR_URL, json=payload, timeout=3.0)
|
||||
|
||||
29
py-client/libs/snapshot.py
Normal file
29
py-client/libs/snapshot.py
Normal file
@@ -0,0 +1,29 @@
|
||||
"""策略账户快照,供采集线程读取。"""
|
||||
|
||||
from copy import deepcopy
|
||||
from threading import Lock
|
||||
|
||||
from sdk import Assets, DealItem, PositionItem
|
||||
|
||||
|
||||
_collector_lock = Lock()
|
||||
_collector_snapshot: tuple[str, Assets, list[PositionItem], list[DealItem]] | None = None
|
||||
|
||||
|
||||
def cache_portfolio(
|
||||
account_id: str,
|
||||
assets: Assets,
|
||||
positions: list[PositionItem],
|
||||
deals: list[DealItem],
|
||||
) -> None:
|
||||
"""整体替换最新快照,策略线程不执行序列化和网络上报。"""
|
||||
global _collector_snapshot
|
||||
with _collector_lock:
|
||||
_collector_snapshot = (account_id, assets, positions, deals)
|
||||
|
||||
|
||||
def get_collector_snapshot() -> tuple[str, Assets, list[PositionItem], list[DealItem]] | None:
|
||||
"""供 scheduler 读取;复制在锁外执行,不阻塞下一轮缓存更新。"""
|
||||
with _collector_lock:
|
||||
snapshot = _collector_snapshot
|
||||
return deepcopy(snapshot)
|
||||
Reference in New Issue
Block a user