How to kill a subprocess in python

独自空忆成欢 提交于 2020-02-02 14:48:46

问题


I have code which runs a webcamera on a linux pc using the gst-launchcommand.

When I kill the process, the webcamera window does not turn off, but the program stops running. I want the webcamera window also to be closed. Can you help me on this?

import subprocess
import time
import os
import signal

cmd = "gst-launch-1.0 -v v4l2src ! video/x-raw,format=YUY2 ! videoconvert ! autovideosink"
process = subprocess.Popen(cmd, shell = True)
time.sleep(5)
#print(subprocess.Popen.pid)
#process.terminate()
os.kill(process.pid, signal.SIGKILL)
#process.kill()

回答1:


Hope it will help you.

import os
import signal
import subprocess

process = subprocess.Popen(cmd, shell = True)
os.killpg(os.getpgid(process.pid), signal.SIGTERM)


来源:https://stackoverflow.com/questions/58536265/how-to-kill-a-subprocess-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!