TLDR I want to kill a subprocess like top while it is still running
I am using Fastapi to run a command on input. For example if I enter top my program runs the command but since it does not return, at the moment I have to use a time delay then kill/terminate it. However I want to be able to kill it while it is still running. However at the moment it won't run my kill command until the time runs out.
Here is the current code for running a process:
@app.put("/command/{command}")
async def run_command(command: str):
subprocess.run([command], shell=True, timeout=10)
return {"run command"}
and to kill it
@app.get("/stop")
async def stop():
proc.kill()
return{"Stop"}
I am new to fastapi so I would be grateful for any help