This commit is contained in:
hailin 2025-07-08 18:15:15 +08:00
parent 1d1cc7eb09
commit 3fb5def6d8
1 changed files with 5 additions and 9 deletions

View File

@ -5,6 +5,7 @@ import threading
import subprocess import subprocess
import gradio as gr import gradio as gr
import psutil import psutil
import signal
# ---------------- 全局进程句柄 ---------------- # ---------------- 全局进程句柄 ----------------
current_process = None current_process = None
@ -44,7 +45,7 @@ def run_eval(
try: try:
current_process = subprocess.Popen( current_process = subprocess.Popen(
command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
text=True, bufsize=1 text=True, bufsize=1, start_new_session=True
) )
for line in current_process.stdout: for line in current_process.stdout:
@ -98,17 +99,12 @@ 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:
try: try:
# 使用 psutil 杀死所有子进程 os.killpg(os.getpgid(current_process.pid), signal.SIGKILL) # ✅ 杀整个进程组
parent = psutil.Process(current_process.pid)
children = parent.children(recursive=True)
for child in children:
child.kill()
parent.kill()
except Exception as e: except Exception as e:
return f"[Error stopping process] {e}\n" return f"[Error stopping process group] {e}\n"
finally: finally:
current_process = None current_process = None
return "[Stopped by user]\n" return "[Force stopped entire process group]\n"
return "[No active process]\n" return "[No active process]\n"