QTrade/test_run.py
cuizhan 67b85ab232 Refactor: Implement WebSocket architecture with separated communication and business logic layers
- Implemented WebSocket protocol v1.0 specification with protocol headers, versioning, and batch support
- Renamed qtrade_server.py -> ws_server.py, client_adapter.py -> ws_client.py to reflect WebSocket implementation
- Created pure communication layer (WSClient) with only connect/send/close functionality
- Created business logic layer (BacktestClientAdapter) for protocol construction and signal parsing
- Updated bt_engine.py to use BacktestClientAdapter for strategy communication
- Fixed datetime formatting issues (time.strftime -> datetime.now().strftime for %f support)
- Fixed WebSocket 1000 OK close code logging issue
- Translated all logger output from Chinese to English for better compatibility
- Successfully tested with 242 K-lines data transmission

Architecture changes:
- networks/ layer: Pure WebSocket communication (no business logic)
- backtest/ layer: Business logic (protocol v1.0 message construction)
- Clear separation of concerns between transport and application layers
2025-12-25 16:15:56 +08:00

66 lines
1.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
测试启动脚本 - 简单版本
先启动服务器,等待一段时间后启动客户端
"""
import subprocess
import sys
import time
from pathlib import Path
# 工作目录
workspace = Path(__file__).parent
def start_server():
"""启动服务器进程(在新的控制台窗口)"""
print("=" * 60)
print("正在启动 QTrade Server(新窗口)...")
print("=" * 60)
subprocess.Popen(
[sys.executable, "-m", "src.networks.server.ws_server"],
cwd=workspace,
creationflags=subprocess.CREATE_NEW_CONSOLE, # Windows下在新控制台窗口启动
)
print("✓ 服务器已在新窗口启动,等待5秒...")
def start_client():
"""启动客户端进程(直接显示输出)"""
print("\n" + "=" * 60)
print("正在启动 Backtest Client...")
print("=" * 60)
subprocess.run([sys.executable, "-m", "src.backtest.main"], cwd=workspace)
def main():
"""主函数"""
if len(sys.argv) > 1 and sys.argv[1] == "server":
# 只启动服务器
start_server()
elif len(sys.argv) > 1 and sys.argv[1] == "client":
# 只启动客户端
start_client()
elif len(sys.argv) > 1 and sys.argv[1] == "both":
# 同时启动服务器和客户端
print("=" * 60)
print("同时启动服务器和客户端")
print("=" * 60)
start_server()
time.sleep(5) # 等待服务器启动
print("\n现在启动客户端...")
start_client()
else:
print("使用方法:")
print(" 启动服务器: python test_run.py server")
print(" 启动客户端: python test_run.py client")
print(" 同时启动: python test_run.py both")
print("\n建议:")
print(" 1. 先运行: python test_run.py server (会弹出新窗口)")
print(" 2. 然后运行: python test_run.py client")
if __name__ == "__main__":
main()