Answer the question
In order to leave comments, you need to log in
How to end a process in python?
Good afternoon.
The bottom line is that I start ffmpeg using subprocess.Popen, and when I terminate the script with proc.kill() then proc.terminate() then sys.exit() , then ffmpeg continues to hang in the task manager, although it is not active (i.e. in the column where the CPU is written, it shows 0). How to remove it completely from the task manager?
Answer the question
In order to leave comments, you need to log in
Solved the problem using the psutil library
import psutil
parent_pid = 30437 # my example
parent = psutil.Process(parent_pid)
for child in parent.children(recursive=True): # or parent.children() for recursive=False
child.kill()
parent.kill()
Probably ffmpeg starts subprocesses. Kill the whole group:
pro = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, preexec_fn=os.setsid)
os.killpg(os.getpgid(pro.pid), signal.SIGTERM)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question