Fix positions and records pages hanging on load.

Stop blocking page render on full trading live rebuild and CTP trade sync; use cached snapshot and background prime instead.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-30 23:31:13 +08:00
parent 1b3a7f1bdc
commit 39eac983ff
2 changed files with 32 additions and 29 deletions
-15
View File
@@ -1442,21 +1442,6 @@ def records():
conn = get_db() conn = get_db()
ctp_sync_info = None ctp_sync_info = None
try:
from ctp_trade_sync import sync_trade_logs_from_ctp
from trading_context import get_account_capital, get_trading_mode
from vnpy_bridge import ctp_status
mode = get_trading_mode(get_setting)
if ctp_status(mode).get("connected"):
capital = get_account_capital(conn, get_setting)
ctp_sync_info = sync_trade_logs_from_ctp(
conn, mode, capital=capital, trading_mode=mode,
)
conn.commit()
except Exception as exc:
app.logger.warning("ctp trade sync on records page: %s", exc)
sql = "SELECT * FROM review_records WHERE 1=1" sql = "SELECT * FROM review_records WHERE 1=1"
params: list = [] params: list = []
if start: if start:
+27 -9
View File
@@ -1822,8 +1822,7 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
def _refresh_trading_live_snapshot(*, fast: bool = False) -> dict: def _refresh_trading_live_snapshot(*, fast: bool = False) -> dict:
mode = get_trading_mode(get_setting) mode = get_trading_mode(get_setting)
if ctp_status(mode).get("connected"): if ctp_status(mode).get("connected") and not fast:
if not fast or trading_state.sync_state == "idle":
try: try:
with _ctp_td_lock: with _ctp_td_lock:
get_bridge().calibrate_trading_state() get_bridge().calibrate_trading_state()
@@ -2077,12 +2076,27 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
_schedule_recommend_refresh() _schedule_recommend_refresh()
ctp_connected = is_ctp_connected(get_setting) ctp_connected = is_ctp_connected(get_setting)
margin_rec = small_account_margin_recommendations() margin_rec = small_account_margin_recommendations()
bootstrap_live: dict = {} bootstrap_live = position_hub.get_snapshot()
try: if not bootstrap_live:
bootstrap_live = _build_trading_live_payload(conn, fast=True) bootstrap_live = {
position_hub.set_snapshot(bootstrap_live) "ok": True,
except Exception as exc: "rows": [],
logger.debug("positions page bootstrap: %s", exc) "active_orders": [],
"pending_orders": [],
"capital": capital,
"ctp_status": dict(ctp_st),
"risk_status": risk,
"trading_session": is_trading_session(),
"night_session": is_night_trading_session(),
"session_clock": trading_session_clock(),
"sync_state": trading_state.sync_state,
"sync_label": trading_state.sync_label(),
}
else:
bootstrap_live = dict(bootstrap_live)
bootstrap_live.setdefault("capital", capital)
bootstrap_live.setdefault("risk_status", risk)
bootstrap_live["ctp_status"] = dict(ctp_st)
return render_template( return render_template(
"trade.html", "trade.html",
trading_mode=mode, trading_mode=mode,
@@ -3878,7 +3892,11 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
def _init_tables(conn): def _init_tables(conn):
init_strategy_tables(conn) init_strategy_tables(conn)
_prime_position_snapshot() threading.Thread(
target=_prime_position_snapshot,
daemon=True,
name="position-prime",
).start()
_pos_refresh_tick = {"n": 0} _pos_refresh_tick = {"n": 0}
_last_full_calibrate = {"ts": 0.0} _last_full_calibrate = {"ts": 0.0}