Repository navigation
Expand file tree
/
Copy pathstart_dev.py
More file actions
159 lines (131 loc) · 4.47 KB
/
Copy pathstart_dev.py
File metadata and controls
159 lines (131 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python
"""
TradingAgents-CN Development Startup Script
Start Backend API and Worker for local development
"""
import os
import sys
import subprocess
import signal
import time
import argparse
from pathlib import Path
from core.python_runtime import resolve_python_executable
# Add project root to path
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))
def find_python_exe():
"""Find Python executable (venv/env/system)"""
return resolve_python_executable(project_root, env_var_names=("TRADINGAGENTS_PYTHON_EXE",))
def main():
parser = argparse.ArgumentParser(
description="TradingAgents-CN Development Startup Script"
)
parser.add_argument(
"--backend-only",
action="store_true",
help="Start Backend only"
)
parser.add_argument(
"--worker-only",
action="store_true",
help="Start Worker only"
)
parser.add_argument(
"--no-worker",
action="store_true",
help="Start Backend without Worker"
)
parser.add_argument(
"--port",
type=int,
default=8000,
help="Backend port (default: 8000)"
)
args = parser.parse_args()
print("=" * 50)
print("TradingAgents-CN Development Mode")
print("=" * 50)
print()
# Find Python
python_exe = find_python_exe()
print(f"[OK] Python: {python_exe}")
# Set UTF-8 encoding
os.environ["PYTHONIOENCODING"] = "utf-8"
os.environ["PYTHONUTF8"] = "1"
os.environ["PYTHONUNBUFFERED"] = "1"
print("[OK] UTF-8 encoding enabled")
print()
# Check required files
backend_main = project_root / "app" / "main.py"
worker_main = project_root / "app" / "worker" / "__main__.py"
if not args.backend_only and not args.worker_only:
if not backend_main.exists():
print(f"[ERROR] Backend not found: {backend_main}")
sys.exit(1)
if not args.no_worker and not worker_main.exists():
print(f"[ERROR] Worker not found: {worker_main}")
sys.exit(1)
# Start processes
processes = []
try:
if not args.worker_only:
print("[1/2] Starting Backend API...")
print(f" Port: {args.port}")
print(f" URL: http://127.0.0.1:{args.port}")
print(f" Docs: http://127.0.0.1:{args.port}/docs")
print()
backend_process = subprocess.Popen(
[python_exe, str(backend_main), "--port", str(args.port)],
cwd=str(project_root),
env=os.environ.copy()
)
processes.append(("Backend", backend_process))
print(f"[OK] Backend started (PID: {backend_process.pid})")
print()
# Wait for backend to start
time.sleep(2)
if not args.backend_only and not args.no_worker:
print("[2/2] Starting Worker...")
print(f" Log: logs/worker.log")
print()
worker_process = subprocess.Popen(
[python_exe, str(worker_main)],
cwd=str(project_root),
env=os.environ.copy()
)
processes.append(("Worker", worker_process))
print(f"[OK] Worker started (PID: {worker_process.pid})")
print()
print("=" * 50)
print("Development server running!")
print("=" * 50)
print()
print("Press Ctrl+C to stop all processes")
print()
# Wait for processes
while True:
time.sleep(1)
# Check if any process exited
for name, proc in processes:
if proc.poll() is not None:
print(f"\n[ERROR] {name} exited with code {proc.returncode}")
raise KeyboardInterrupt
except KeyboardInterrupt:
print("\n[INFO] Shutting down...")
finally:
# Stop all processes
for name, proc in processes:
if proc.poll() is None:
print(f"Stopping {name} (PID: {proc.pid})...")
try:
proc.terminate()
proc.wait(timeout=5)
except subprocess.TimeoutExpired:
print(f"Force killing {name}...")
proc.kill()
print()
print("[OK] All processes stopped")
print()
if __name__ == "__main__":
main()