#!/usr/bin/env python3 # -*- coding: utf-8 -*- """作业13 · 一键拉起 primary/backup 两个模拟上游(学习用) 用法: python hw13_02_启动双上游.py # Ctrl+C 结束 """ from __future__ import annotations import subprocess import sys import time from pathlib import Path HERE = Path(__file__).resolve().parent MOCK = HERE / "hw13_01_模拟上游.py" def main() -> int: py = sys.executable procs = [ subprocess.Popen( [py, str(MOCK), "--port", "18101", "--name", "primary", "--max-concurrent", "4", "--latency-ms", "80"], ), subprocess.Popen( [py, str(MOCK), "--port", "18102", "--name", "backup", "--max-concurrent", "20", "--latency-ms", "50"], ), ] print("[OK] primary http://127.0.0.1:18101/v1 max_concurrent=4") print("[OK] backup http://127.0.0.1:18102/v1 max_concurrent=20") print("[INFO] 另开终端启动测试 LiteLLM(可选):见 patches_cursor/作业13_虚拟模型主备/config.yaml") print("[INFO] Ctrl+C 停止双上游") try: while True: time.sleep(1) for p in procs: if p.poll() is not None: print(f"[WARN] 子进程退出 code={p.returncode}") raise KeyboardInterrupt except KeyboardInterrupt: print("\n[STOP] 结束模拟上游") for p in procs: p.terminate() for p in procs: try: p.wait(timeout=5) except Exception: p.kill() return 0 if __name__ == "__main__": raise SystemExit(main())