【发布时间】:2021-12-26 19:34:46
【问题描述】:
TLDR 我想杀死一个像 top 这样仍在运行的子进程
我正在使用 Fastapi 在输入上运行命令。例如,如果我输入 top 我的程序运行命令,但由于它没有返回,此时我必须使用时间延迟然后杀死/终止它。但是我希望能够在它仍在运行时将其杀死。但是目前它不会运行我的 kill 命令,直到时间用完。
这是当前运行进程的代码:
@app.put("/command/{command}")
async def run_command(command: str):
subprocess.run([command], shell=True, timeout=10)
return {"run command"}
杀死它
@app.get("/stop")
async def stop():
proc.kill()
return{"Stop"}
我是 fastapi 的新手,所以我会很感激任何帮助
【问题讨论】:
标签: python subprocess fastapi