fix: stabilize long-running quote refresh

This commit is contained in:
王鹏
2026-11-09 10:42:08 +08:00
parent b7e0894f4b
commit 5f4df03581
6 changed files with 281 additions and 84 deletions

View File

@@ -20,7 +20,14 @@ a = Analysis(
# PATH, which can make PyInstaller collect those incompatible DLLs by name. # PATH, which can make PyInstaller collect those incompatible DLLs by name.
# If bundled, they shadow Windows' ICU DLLs and PySide6.QtCore fails to load # If bundled, they shadow Windows' ICU DLLs and PySide6.QtCore fails to load
# with ERROR_PROC_NOT_FOUND during startup. # with ERROR_PROC_NOT_FOUND during startup.
_excluded_runtime_dlls = {"icuuc.dll", "icudt78.dll"} _excluded_runtime_dlls = {
"icuuc.dll",
"icudt78.dll",
# Windows 10 already supplies the Universal CRT. Bundling another copy
# makes the one-file process load both the system and private ucrtbase,
# which can corrupt native heaps when Qt objects cross DLL boundaries.
"ucrtbase.dll",
}
a.binaries = [ a.binaries = [
entry entry
for entry in a.binaries for entry in a.binaries

View File

@@ -58,15 +58,28 @@ class TencentQuoteProvider:
self.retries = retries self.retries = retries
self._fetcher = fetcher or _default_fetcher self._fetcher = fetcher or _default_fetcher
def fetch_quotes(self, instruments: Iterable[Instrument]) -> dict[str, QuoteSnapshot]: @property
def uses_default_fetcher(self) -> bool:
"""Whether the provider can be driven by Qt's asynchronous network API."""
return self._fetcher is _default_fetcher
def build_request_url(self, instruments: Iterable[Instrument]) -> str | None:
symbols = [infer_tencent_symbol(instrument.code) for instrument in instruments] symbols = [infer_tencent_symbol(instrument.code) for instrument in instruments]
if not symbols: if not symbols:
return {} return None
query = ",".join(symbols) return f"{self.endpoint}{','.join(symbols)}"
url = f"{self.endpoint}{query}"
raw = self._fetch_with_retry(url) @staticmethod
def parse_response(raw: bytes) -> dict[str, QuoteSnapshot]:
return {quote.code: quote for quote in parse_tencent_response(raw)} return {quote.code: quote for quote in parse_tencent_response(raw)}
def fetch_quotes(self, instruments: Iterable[Instrument]) -> dict[str, QuoteSnapshot]:
url = self.build_request_url(instruments)
if url is None:
return {}
raw = self._fetch_with_retry(url)
return self.parse_response(raw)
def _fetch_with_retry(self, url: str) -> bytes: def _fetch_with_retry(self, url: str) -> bytes:
last_error: Exception | None = None last_error: Exception | None = None
for _ in range(self.retries + 1): for _ in range(self.retries + 1):

View File

@@ -4,7 +4,7 @@ import sys
from decimal import Decimal from decimal import Decimal
from pathlib import Path from pathlib import Path
from PySide6.QtCore import Qt from PySide6.QtCore import QSignalBlocker, Qt
from PySide6.QtGui import QAction, QBrush, QColor, QFont, QIcon from PySide6.QtGui import QAction, QBrush, QColor, QFont, QIcon
from PySide6.QtWidgets import ( from PySide6.QtWidgets import (
QAbstractItemView, QAbstractItemView,
@@ -477,14 +477,30 @@ class MainWindow(QMainWindow):
except Exception as exc: except Exception as exc:
QMessageBox.warning(self, "刷新失败", str(exc)) QMessageBox.warning(self, "刷新失败", str(exc))
return return
tables = (
self.holdings_table,
self.grid_levels_table,
self.open_grid_lots_table,
self.grid_matches_table,
self.trades_table,
)
for table in tables:
table.setUpdatesEnabled(False)
try:
self.summary_labels["total_assets"].setText(format_money(account_summary.total_assets)) self.summary_labels["total_assets"].setText(format_money(account_summary.total_assets))
self.summary_labels["cash"].setText(format_money(account_summary.cash)) self.summary_labels["cash"].setText(format_money(account_summary.cash))
self.summary_labels["market_value"].setText(format_money(account_summary.market_value)) self.summary_labels["market_value"].setText(format_money(account_summary.market_value))
self.summary_labels["floating_pnl"].setText(format_money(account_summary.floating_pnl)) self.summary_labels["floating_pnl"].setText(format_money(account_summary.floating_pnl))
self._style_pnl_label(self.summary_labels["floating_pnl"], account_summary.floating_pnl) self._style_pnl_label(
self.summary_labels["floating_pnl"],
account_summary.floating_pnl,
)
self.summary_labels["usage"].setText(format_percent(account_summary.capital_usage_rate)) self.summary_labels["usage"].setText(format_percent(account_summary.capital_usage_rate))
self._fill_holdings_table() self._fill_holdings_table()
self._refresh_details() self._refresh_details()
finally:
for table in tables:
table.setUpdatesEnabled(True)
def _refresh_quotes(self) -> None: def _refresh_quotes(self) -> None:
self.quote_monitor.refresh(manual=True) self.quote_monitor.refresh(manual=True)
@@ -507,7 +523,10 @@ class MainWindow(QMainWindow):
self.refresh_quotes_button.setText("刷新中..." if refreshing else "刷新行情") self.refresh_quotes_button.setText("刷新中..." if refreshing else "刷新行情")
def _fill_holdings_table(self) -> None: def _fill_holdings_table(self) -> None:
blocker = QSignalBlocker(self.holdings_table)
try:
self.holdings_table.setRowCount(len(self._positions)) self.holdings_table.setRowCount(len(self._positions))
selected_row = -1
for row, position in enumerate(self._positions): for row, position in enumerate(self._positions):
values = [ values = [
position.code, position.code,
@@ -531,8 +550,18 @@ class MainWindow(QMainWindow):
item = self._make_item(value, pnl_values.get(column)) item = self._make_item(value, pnl_values.get(column))
item.setData(Qt.ItemDataRole.UserRole, position.instrument_id) item.setData(Qt.ItemDataRole.UserRole, position.instrument_id)
self.holdings_table.setItem(row, column, item) self.holdings_table.setItem(row, column, item)
if self._positions and self.holdings_table.currentRow() < 0: if position.instrument_id == self._selected_instrument_id:
self.holdings_table.selectRow(0) selected_row = row
if selected_row < 0 and self._positions:
selected_row = 0
self._selected_instrument_id = self._positions[0].instrument_id
elif not self._positions:
self._selected_instrument_id = None
if selected_row >= 0:
self.holdings_table.selectRow(selected_row)
finally:
del blocker
def _on_holding_selected(self) -> None: def _on_holding_selected(self) -> None:
row = self.holdings_table.currentRow() row = self.holdings_table.currentRow()

View File

@@ -1,7 +1,9 @@
from __future__ import annotations from __future__ import annotations
from PySide6.QtCore import QObject, Qt, QThread, QTimer, Signal from PySide6.QtCore import QObject, Qt, QThread, QTimer, QUrl, Signal, Slot
from PySide6.QtNetwork import QNetworkAccessManager, QNetworkReply, QNetworkRequest
from grid_trading.market.tencent import QuoteFetchError, TencentQuoteProvider
from grid_trading.services.trading_service import TradingService from grid_trading.services.trading_service import TradingService
@@ -9,14 +11,14 @@ class QuoteRefreshWorker(QObject):
finished = Signal(object) finished = Signal(object)
failed = Signal(str) failed = Signal(str)
def __init__(self, service: TradingService, instruments): def __init__(self, service: TradingService):
super().__init__() super().__init__()
self._service = service self._service = service
self._instruments = instruments
def run(self) -> None: @Slot(object)
def run(self, instruments) -> None:
try: try:
quotes = self._service.fetch_quotes_for_instruments(self._instruments) quotes = self._service.fetch_quotes_for_instruments(instruments)
except Exception as exc: except Exception as exc:
self.failed.emit(str(exc)) self.failed.emit(str(exc))
return return
@@ -24,6 +26,7 @@ class QuoteRefreshWorker(QObject):
class QuoteMonitorController(QObject): class QuoteMonitorController(QObject):
_refresh_requested = Signal(object)
refresh_started = Signal() refresh_started = Signal()
refresh_finished = Signal() refresh_finished = Signal()
quotes_updated = Signal(object) quotes_updated = Signal(object)
@@ -44,15 +47,24 @@ class QuoteMonitorController(QObject):
self._thread: QThread | None = None self._thread: QThread | None = None
self._worker: QuoteRefreshWorker | None = None self._worker: QuoteRefreshWorker | None = None
self._manual_refresh = False self._manual_refresh = False
self._refreshing = False
self._failure_count = 0 self._failure_count = 0
self._shutting_down = False self._shutting_down = False
self._network_manager = QNetworkAccessManager(self)
self._network_reply: QNetworkReply | None = None
self._network_url: str | None = None
self._network_retries_left = 0
self._network_timed_out = False
self._network_timeout = QTimer(self)
self._network_timeout.setSingleShot(True)
self._network_timeout.timeout.connect(self._abort_timed_out_request)
self._timer = QTimer(self) self._timer = QTimer(self)
self._timer.setInterval(max(3_000, interval_ms)) self._timer.setInterval(max(3_000, interval_ms))
self._timer.timeout.connect(self.refresh) self._timer.timeout.connect(self.refresh)
@property @property
def is_refreshing(self) -> bool: def is_refreshing(self) -> bool:
return self._thread is not None and self._thread.isRunning() return self._refreshing
def start(self) -> None: def start(self) -> None:
self._shutting_down = False self._shutting_down = False
@@ -65,7 +77,7 @@ class QuoteMonitorController(QObject):
self._timer.setInterval(max(3_000, interval_ms)) self._timer.setInterval(max(3_000, interval_ms))
def refresh(self, manual: bool = False) -> bool: def refresh(self, manual: bool = False) -> bool:
if self.is_refreshing: if self._shutting_down or self.is_refreshing:
return False return False
try: try:
instruments = self.service.list_instruments() instruments = self.service.list_instruments()
@@ -77,28 +89,109 @@ class QuoteMonitorController(QObject):
self.evaluate_now() self.evaluate_now()
return False return False
provider = self.service.quote_provider
network_url: str | None = None
use_qt_network = (
isinstance(provider, TencentQuoteProvider)
and provider.uses_default_fetcher
)
if use_qt_network:
try:
network_url = provider.build_request_url(instruments)
except Exception as exc:
self.refresh_failed.emit(str(exc), manual)
return False
self._manual_refresh = manual self._manual_refresh = manual
self._refreshing = True
self.refresh_started.emit()
if use_qt_network and network_url is not None:
self._network_url = network_url
self._network_retries_left = max(0, provider.retries)
self._start_network_request()
else:
# Keep the worker fallback for injected/custom providers used by
# callers and tests. The packaged application uses Qt networking,
# so it does not keep a native worker thread alive all day.
self._ensure_worker_thread()
self._refresh_requested.emit(instruments)
return True
def _start_network_request(self) -> None:
if self._network_url is None:
self._handle_failure("行情请求地址为空")
return
provider = self.service.quote_provider
self._network_timed_out = False
request = QNetworkRequest(QUrl(self._network_url))
reply = self._network_manager.get(request)
self._network_reply = reply
reply.finished.connect(self._handle_network_finished)
timeout_ms = max(1_000, int(provider.timeout * 1_000))
self._network_timeout.start(timeout_ms)
def _abort_timed_out_request(self) -> None:
reply = self._network_reply
if reply is not None and reply.isRunning():
self._network_timed_out = True
reply.abort()
def _handle_network_finished(self) -> None:
reply = self._network_reply
if reply is None:
return
self._network_timeout.stop()
self._network_reply = None
if self._shutting_down:
reply.deleteLater()
self._finish_refresh()
return
error = reply.error()
if error != QNetworkReply.NetworkError.NoError:
message = (
"行情请求超时"
if self._network_timed_out
else f"Tencent quote request failed: {reply.errorString()}"
)
reply.deleteLater()
if self._network_retries_left > 0:
self._network_retries_left -= 1
self._start_network_request()
return
self._handle_failure(message)
return
raw = bytes(reply.readAll())
reply.deleteLater()
try:
provider = self.service.quote_provider
if not isinstance(provider, TencentQuoteProvider):
raise QuoteFetchError("行情提供器已变更")
quotes = provider.parse_response(raw)
except Exception as exc:
self._handle_failure(str(exc))
return
self._handle_success(quotes)
def _ensure_worker_thread(self) -> None:
if self._thread is not None and self._thread.isRunning():
return
self._thread = QThread(self) self._thread = QThread(self)
self._worker = QuoteRefreshWorker(self.service, instruments) self._worker = QuoteRefreshWorker(self.service)
self._worker.moveToThread(self._thread) self._worker.moveToThread(self._thread)
self._thread.started.connect(self._worker.run) self._refresh_requested.connect(
self._worker.run,
Qt.ConnectionType.QueuedConnection,
)
self._worker.finished.connect(self._handle_success) self._worker.finished.connect(self._handle_success)
self._worker.failed.connect(self._handle_failure) self._worker.failed.connect(self._handle_failure)
self._worker.finished.connect(
self._thread.quit,
Qt.ConnectionType.DirectConnection,
)
self._worker.failed.connect(
self._thread.quit,
Qt.ConnectionType.DirectConnection,
)
self._worker.finished.connect(self._worker.deleteLater)
self._worker.failed.connect(self._worker.deleteLater)
self._thread.finished.connect(self._on_thread_finished) self._thread.finished.connect(self._on_thread_finished)
self._thread.finished.connect(self._worker.deleteLater)
self._thread.finished.connect(self._thread.deleteLater) self._thread.finished.connect(self._thread.deleteLater)
self.refresh_started.emit()
self._thread.start() self._thread.start()
return True
def evaluate_now(self) -> None: def evaluate_now(self) -> None:
try: try:
@@ -113,45 +206,67 @@ class QuoteMonitorController(QObject):
def shutdown(self) -> bool: def shutdown(self) -> bool:
"""Stop scheduling refreshes without blocking the GUI thread. """Stop scheduling refreshes without blocking the GUI thread.
A running urllib request cannot be interrupted by ``QThread.quit()``. A running urllib request cannot be interrupted. Let it finish in the
Let it finish in the background and notify the window when it is safe persistent worker thread, then stop that thread and notify the window
to complete application shutdown instead of waiting synchronously. when it is safe to complete application shutdown.
""" """
self._shutting_down = True self._shutting_down = True
self._timer.stop() self._timer.stop()
self._network_timeout.stop()
reply = self._network_reply
if reply is not None:
self._network_reply = None
try:
reply.finished.disconnect(self._handle_network_finished)
except (RuntimeError, TypeError):
pass
reply.abort()
reply.deleteLater()
self._refreshing = False
self._manual_refresh = False
self.refresh_finished.emit()
thread = self._thread thread = self._thread
if thread is not None and thread.isRunning(): if thread is None or not thread.isRunning():
thread.quit()
return False
self._thread = None self._thread = None
self._worker = None self._worker = None
return True return True
if not self._refreshing:
thread.quit()
return False
def _handle_success(self, quotes: dict) -> None: def _handle_success(self, quotes: dict) -> None:
if self._shutting_down: if not self._shutting_down:
return
try: try:
self.service.apply_quote_snapshots(quotes) self.service.apply_quote_snapshots(quotes)
except Exception as exc: except Exception as exc:
self._handle_failure(str(exc)) self._record_failure(str(exc))
return else:
self._failure_count = 0 self._failure_count = 0
self.quotes_updated.emit(quotes) self.quotes_updated.emit(quotes)
self.evaluate_now() self.evaluate_now()
self._finish_refresh()
def _handle_failure(self, message: str) -> None: def _handle_failure(self, message: str) -> None:
if self._shutting_down: if not self._shutting_down:
return self._record_failure(message)
self._finish_refresh()
def _record_failure(self, message: str) -> None:
self._failure_count += 1 self._failure_count += 1
if self._failure_count >= 2: if self._failure_count >= 2:
self.service.clear_quote_snapshots() self.service.clear_quote_snapshots()
self.evaluate_now() self.evaluate_now()
self.refresh_failed.emit(message, self._manual_refresh) self.refresh_failed.emit(message, self._manual_refresh)
def _finish_refresh(self) -> None:
self._refreshing = False
self._manual_refresh = False
self.refresh_finished.emit()
if self._shutting_down and self._thread is not None:
self._thread.quit()
def _on_thread_finished(self) -> None: def _on_thread_finished(self) -> None:
self._thread = None self._thread = None
self._worker = None self._worker = None
self._manual_refresh = False
self.refresh_finished.emit()
if self._shutting_down: if self._shutting_down:
self.shutdown_finished.emit() self.shutdown_finished.emit()

View File

@@ -5,6 +5,7 @@ import pytest
from grid_trading.domain.models import Instrument from grid_trading.domain.models import Instrument
from grid_trading.market.tencent import ( from grid_trading.market.tencent import (
QuoteFetchError, QuoteFetchError,
TENCENT_ENDPOINT,
TencentQuoteProvider, TencentQuoteProvider,
infer_tencent_symbol, infer_tencent_symbol,
parse_tencent_response, parse_tencent_response,
@@ -18,6 +19,20 @@ def test_infer_tencent_symbol_from_a_share_code():
assert infer_tencent_symbol("sz000001") == "sz000001" assert infer_tencent_symbol("sz000001") == "sz000001"
def test_provider_builds_request_url_for_async_qt_client():
provider = TencentQuoteProvider()
url = provider.build_request_url(
[
Instrument(id=1, code="000001", name="平安银行"),
Instrument(id=2, code="510300", name="沪深300ETF"),
]
)
assert provider.uses_default_fetcher
assert url == f"{TENCENT_ENDPOINT}sz000001,sh510300"
def test_parse_tencent_response_extracts_realtime_fields(): def test_parse_tencent_response_extracts_realtime_fields():
raw = ( raw = (
'v_sz000001="51~平安银行~000001~10.60~10.47~10.44~0~0~0~' 'v_sz000001="51~平安银行~000001~10.60~10.47~10.44~0~0~0~'

View File

@@ -410,7 +410,25 @@ def test_quote_refresh_does_not_block_main_window(tmp_path, monkeypatch):
assert not window.quote_monitor.is_refreshing assert not window.quote_monitor.is_refreshing
worker_thread = window.quote_monitor._thread
assert worker_thread is not None
assert worker_thread.isRunning()
assert window.quote_monitor.refresh()
deadline = time.perf_counter() + 2
while window.quote_monitor.is_refreshing and time.perf_counter() < deadline:
app.processEvents()
time.sleep(0.01)
assert not window.quote_monitor.is_refreshing
assert window.quote_monitor._thread is worker_thread
window.close() window.close()
deadline = time.perf_counter() + 2
while window._close_pending and time.perf_counter() < deadline:
app.processEvents()
time.sleep(0.01)
assert not window._close_pending
service.close() service.close()
app.processEvents() app.processEvents()