lanny xu commited on
Commit
af119c2
·
1 Parent(s): 4ba4388

optimize query speed

Browse files
Files changed (1) hide show
  1. run_debug.py +93 -0
run_debug.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import subprocess
2
+ import time
3
+ import sys
4
+ import os
5
+ import threading
6
+
7
+ def stream_reader(pipe, prefix):
8
+ """实时读取并打印子进程输出"""
9
+ try:
10
+ with pipe:
11
+ for line in iter(pipe.readline, ''):
12
+ print(f"[{prefix}] {line.strip()}")
13
+ # 检查是否有启动成功的标志
14
+ if "Uvicorn running on" in line:
15
+ print("✅ 检测到服务器启动成功标志!")
16
+ except Exception:
17
+ pass
18
+
19
+ def run_all_in_one():
20
+ print("🚀 开始集成运行流程...")
21
+
22
+ # 1. 启动服务器 (使用 threading 而不是 shell &)
23
+ print(" 正在启动 FastAPI 服务器...")
24
+ server_process = subprocess.Popen(
25
+ [sys.executable, "server.py"],
26
+ stdout=subprocess.PIPE,
27
+ stderr=subprocess.STDOUT,
28
+ text=True,
29
+ bufsize=1
30
+ )
31
+
32
+ # 启动一个线程来实时打印服务器日志,防止缓冲区满卡死
33
+ t = threading.Thread(target=stream_reader, args=(server_process.stdout, "Server"))
34
+ t.daemon = True
35
+ t.start()
36
+
37
+ # 2. 等待一段时间让服务器初始化
38
+ print("⏳ 等待服务器初始化 (15秒)...")
39
+ time.sleep(15)
40
+
41
+ # 3. 检查服务器进程是否还活着
42
+ if server_process.poll() is not None:
43
+ print(f"❌ 服务器进程已退出 (Exit Code: {server_process.returncode})")
44
+ print(" 请检查上方的 [Server] 日志")
45
+ return
46
+
47
+ # 4. 启动 Cloudflare 隧道
48
+ if not os.path.exists("./cloudflared"):
49
+ print("⚠️ 未找到 cloudflared,正在下载...")
50
+ subprocess.run("wget -q -O cloudflared https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64 && chmod +x cloudflared", shell=True)
51
+
52
+ print("\n🌐 启动 Cloudflare 隧道...")
53
+ tunnel_process = subprocess.Popen(
54
+ ["./cloudflared", "tunnel", "--url", "http://localhost:8000", "--no-autoupdate"],
55
+ stdout=subprocess.PIPE,
56
+ stderr=subprocess.STDOUT,
57
+ text=True,
58
+ bufsize=1
59
+ )
60
+
61
+ # 5. 实时读取隧道输出,提取链接
62
+ print("🔍 寻找公网链接中...")
63
+ try:
64
+ while True:
65
+ line = tunnel_process.stdout.readline()
66
+ if not line:
67
+ break
68
+
69
+ # 打印隧道日志
70
+ # print(f"[Tunnel] {line.strip()}")
71
+
72
+ if "trycloudflare.com" in line:
73
+ import re
74
+ url_match = re.search(r"https?://[\w\.-]+trycloudflare\.com", line)
75
+ if url_match:
76
+ print("\n" + "="*60)
77
+ print("🎉 成功建立隧道!")
78
+ print(f"👉 公网访问地址: {url_match.group(0)}")
79
+ print("="*60 + "\n")
80
+ # 找到链接后,我们继续保持运行,不退出
81
+
82
+ # 检查服务器是否还在运行
83
+ if server_process.poll() is not None:
84
+ print("❌ 警告:服务器进程意外退出!")
85
+ break
86
+
87
+ except KeyboardInterrupt:
88
+ print("正在停止服务...")
89
+ server_process.terminate()
90
+ tunnel_process.terminate()
91
+
92
+ if __name__ == "__main__":
93
+ run_all_in_one()