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
+32 -14
View File
@@ -1822,13 +1822,12 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
def _refresh_trading_live_snapshot(*, fast: bool = False) -> dict:
mode = get_trading_mode(get_setting)
if ctp_status(mode).get("connected"):
if not fast or trading_state.sync_state == "idle":
try:
with _ctp_td_lock:
get_bridge().calibrate_trading_state()
except Exception as exc:
logger.debug("refresh calibrate: %s", exc)
if ctp_status(mode).get("connected") and not fast:
try:
with _ctp_td_lock:
get_bridge().calibrate_trading_state()
except Exception as exc:
logger.debug("refresh calibrate: %s", exc)
for p in trading_state.get_positions() or _ctp_positions(mode, refresh_if_empty=False):
ths = _ctp_pos_to_ths_code(p)
if ths:
@@ -2077,12 +2076,27 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
_schedule_recommend_refresh()
ctp_connected = is_ctp_connected(get_setting)
margin_rec = small_account_margin_recommendations()
bootstrap_live: dict = {}
try:
bootstrap_live = _build_trading_live_payload(conn, fast=True)
position_hub.set_snapshot(bootstrap_live)
except Exception as exc:
logger.debug("positions page bootstrap: %s", exc)
bootstrap_live = position_hub.get_snapshot()
if not bootstrap_live:
bootstrap_live = {
"ok": True,
"rows": [],
"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(
"trade.html",
trading_mode=mode,
@@ -3878,7 +3892,11 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
def _init_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}
_last_full_calibrate = {"ts": 0.0}