gradio启动cmd并尝试kill
import gradio as gr
import subprocessall_pid = []def get_all_pid():return str(all_pid)def train_0():proc = subprocess.Popen(['python', 'a.py'], cwd='py_folder_path')all_pid.append(proc.pid)def train_1():proc = subprocess.Popen(['python', 'b.py'],cwd='py_folder_path')all_pid.append(proc.pid)css = """
#warning {background-color: #FFCCCB}
.feedback textarea {font-size: 24px !important}
"""
# 0. 设置整个gradio项目block
gradio_project = gr.Blocks(css=css)
train_button_0 = gr.Button(value="train_0")
train_botton_1 = gr.Button(value="trian_1")
update_pid_button = gr.Button(value="更新存在的任务pid")train_all_pid_gr = gr.Textbox(label="所有已存在的pid")
with gradio_project:train_button_0.render()train_button_0.click(fn=train_0, inputs=None, outputs=None)train_botton_1.render()train_botton_1.click(fn=train_1, inputs=None, outputs=None)update_pid_button.render()train_all_pid_gr.render()update_pid_button.click(fn=get_all_pid, inputs=None, outputs=[train_all_pid_gr])
gradio_project.queue(concurrency_count=4, max_size=3).launch(share=True,server_port=6093)
在这里尝试使用gradio,按钮点击时在后台启动一个新的cmd,并执行特定命令,并返回新的cmd的pid,在窗口上,会回显后台cmd的输出,
以新的cmd窗口启动命令时,可以使用多线程读取数据,在kill时,只需要pkill -P 父进程的pid,就会将所有子进程都杀掉,并保留主进程的cmd窗口。
更详细关于父进程和子进程的关系可参考此链接
Linux终端与进程的关系