W
W
WorldEn2017-01-13 15:33:47
Python
WorldEn, 2017-01-13 15:33:47

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

2 answer(s)
W
WorldEn, 2017-01-13
@WorldEn

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()

R
Roman Mindlin, 2017-01-13
@kgbplus

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 question

Ask a Question

731 491 924 answers to any question