This commit is contained in:
hailin 2025-07-08 18:08:00 +08:00
parent 80d8543f2a
commit 1d1cc7eb09
1 changed files with 13 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import glob
import threading import threading
import subprocess import subprocess
import gradio as gr import gradio as gr
import psutil
# ---------------- 全局进程句柄 ---------------- # ---------------- 全局进程句柄 ----------------
current_process = None current_process = None
@ -96,12 +97,22 @@ def run_eval(
def stop_eval(): def stop_eval():
global current_process global current_process
if current_process and current_process.poll() is None: if current_process and current_process.poll() is None:
current_process.terminate() try:
current_process = None # 使用 psutil 杀死所有子进程
parent = psutil.Process(current_process.pid)
children = parent.children(recursive=True)
for child in children:
child.kill()
parent.kill()
except Exception as e:
return f"[Error stopping process] {e}\n"
finally:
current_process = None
return "[Stopped by user]\n" return "[Stopped by user]\n"
return "[No active process]\n" return "[No active process]\n"
# ---------------- Run/Stop 控制器 ---------------- # ---------------- Run/Stop 控制器 ----------------
def toggle_run( def toggle_run(
inputs, native, other, output_choices, inputs, native, other, output_choices,