From 39eac983ff288e20dcb5548dce59c0dd608e8511 Mon Sep 17 00:00:00 2001 From: dekun Date: Tue, 30 Jun 2026 23:31:13 +0800 Subject: [PATCH] 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 --- app.py | 15 --------------- install_trading.py | 46 ++++++++++++++++++++++++++++++++-------------- 2 files changed, 32 insertions(+), 29 deletions(-) diff --git a/app.py b/app.py index 12a9cf5..899a9d3 100644 --- a/app.py +++ b/app.py @@ -1442,21 +1442,6 @@ def records(): conn = get_db() 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" params: list = [] if start: diff --git a/install_trading.py b/install_trading.py index 486e3ed..7538acb 100644 --- a/install_trading.py +++ b/install_trading.py @@ -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}