Kill process in Linux platform

Add a function to kill process and its children recusively in Linux platform.
This commit is contained in:
Ke 2024-01-18 14:55:38 +08:00 committed by GitHub
parent 515cf3d75e
commit 3a167888e2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,5 +1,8 @@
import json,yaml,warnings,torch import json,yaml,warnings,torch
import platform import platform
import psutil
import os
import signal
warnings.filterwarnings("ignore") warnings.filterwarnings("ignore")
torch.manual_seed(233333) torch.manual_seed(233333)
@ -30,7 +33,7 @@ from scipy.io import wavfile
from tools.my_utils import load_audio from tools.my_utils import load_audio
from multiprocessing import cpu_count from multiprocessing import cpu_count
n_cpu=cpu_count() n_cpu=cpu_count()
# 判断是否有能用来训练和加速推理的N卡 # 判断是否有能用来训练和加速推理的N卡
ngpu = torch.cuda.device_count() ngpu = torch.cuda.device_count()
gpu_infos = [] gpu_infos = []
@ -78,15 +81,33 @@ p_uvr5=None
p_asr=None p_asr=None
p_tts_inference=None p_tts_inference=None
def kill_proc_tree(pid, including_parent=True):
try:
parent = psutil.Process(pid)
except psutil.NoSuchProcess:
# Process already terminated
return
children = parent.children(recursive=True)
for child in children:
try:
os.kill(child.pid, signal.SIGTERM) # or signal.SIGKILL
except OSError:
pass
if including_parent:
try:
os.kill(parent.pid, signal.SIGTERM) # or signal.SIGKILL
except OSError:
pass
system=platform.system() system=platform.system()
def kill_process(pid): def kill_process(pid):
if(system=="Windows"): if(system=="Windows"):
cmd = "taskkill /t /f /pid %s" % pid cmd = "taskkill /t /f /pid %s" % pid
os.system(cmd)
else: else:
cmd = "kill -9 %s"%pid kill_proc_tree(pid)
print(cmd)
os.system(cmd)###linux上杀了webui可能还会没杀干净。。。
# os.kill(p_label.pid,19)#主进程#控制台进程#python子进程###不好使连主进程的webui一起关了辣鸡
def change_label(if_label,path_list): def change_label(if_label,path_list):
global p_label global p_label