Spaces:
Paused
Paused
lanny xu
commited on
Commit
·
4ba4388
1
Parent(s):
92c9888
optimize query speed
Browse files- run_in_kaggle.py +61 -0
run_in_kaggle.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import subprocess
|
| 2 |
+
import time
|
| 3 |
+
import sys
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
def run_background():
|
| 7 |
+
print("🚀 正在后台启动服务...")
|
| 8 |
+
|
| 9 |
+
# 1. 启动服务器 (输出重定向到 server.log)
|
| 10 |
+
server_log = open("server.log", "w")
|
| 11 |
+
server_process = subprocess.Popen(
|
| 12 |
+
[sys.executable, "server.py"],
|
| 13 |
+
stdout=server_log,
|
| 14 |
+
stderr=subprocess.STDOUT
|
| 15 |
+
)
|
| 16 |
+
print(f"✅ 服务器已启动 (PID: {server_process.pid})")
|
| 17 |
+
|
| 18 |
+
# 2. 等待服务器就绪
|
| 19 |
+
print("⏳ 等待服务器启动 (约10秒)...")
|
| 20 |
+
time.sleep(10)
|
| 21 |
+
|
| 22 |
+
# 3. 启动 Cloudflare 隧道
|
| 23 |
+
if not os.path.exists("./cloudflared"):
|
| 24 |
+
print("❌ 未找到 cloudflared,请先运行下载命令")
|
| 25 |
+
return
|
| 26 |
+
|
| 27 |
+
print("🌐 正在建立公网连接...")
|
| 28 |
+
tunnel_process = subprocess.Popen(
|
| 29 |
+
["./cloudflared", "tunnel", "--url", "http://localhost:8000", "--no-autoupdate"],
|
| 30 |
+
stdout=subprocess.PIPE,
|
| 31 |
+
stderr=subprocess.STDOUT,
|
| 32 |
+
text=True
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# 4. 读取并打印访问链接
|
| 36 |
+
print("🔍 正在获取访问地址...")
|
| 37 |
+
found_url = False
|
| 38 |
+
while True:
|
| 39 |
+
line = tunnel_process.stdout.readline()
|
| 40 |
+
if not line:
|
| 41 |
+
break
|
| 42 |
+
|
| 43 |
+
# 打印隧道日志以便调试
|
| 44 |
+
# print(f"[Tunnel] {line.strip()}")
|
| 45 |
+
|
| 46 |
+
if "trycloudflare.com" in line:
|
| 47 |
+
import re
|
| 48 |
+
url_match = re.search(r"https?://[\w\.-]+trycloudflare\.com", line)
|
| 49 |
+
if url_match:
|
| 50 |
+
print("\n" + "="*50)
|
| 51 |
+
print("🎉 成功!请访问以下地址:")
|
| 52 |
+
print(f"👉 {url_match.group(0)}")
|
| 53 |
+
print("="*50 + "\n")
|
| 54 |
+
found_url = True
|
| 55 |
+
break
|
| 56 |
+
|
| 57 |
+
if not found_url:
|
| 58 |
+
print("⚠️ 未能获取到公网地址,请检查 server.log")
|
| 59 |
+
|
| 60 |
+
if __name__ == "__main__":
|
| 61 |
+
run_background()
|