如何终止由subprocess.Popen在Python中运行的Subprocess(带有代码)

时间:2019-04-18 03:59:47

标签: python subprocess kill

首先,我想对自己的语言能力表示歉意,我想尽我所能清楚地描述它。

[Python在Raspberri Pi3 B +上运行]

我正试图杀死由subprocess.popen运行的子进程,我已经厌倦了搜索所有网络,但是所有解决方案都对我不起作用。我是初学者,不确定我错过了什么,我认为最好是有人可以看到我的代码

这里我有运行子进程的代码

pid = subprocess.Popen([sys.executable,"01-Subprogram-serial.py"])

01-Subprogram-serial.py <<我的子程序

这是通过raspberri pi 3 b +同时进行的视频播放和LED控制

我正在使用Tkinter来运行用于播放视频的按钮命令并通过串行设备控制来运行子程序,该子程序仅在首次运行的程序中运行良好,但是由于我无法杀死它,因此即使我更改为另一个命令,子程序仍在运行。

from tkinter import *
from tkinter import messagebox
import os
import sys
import subprocess
import signal
from subprocess import Popen, PIPE
import serial
import time
ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=.5)
top = Tk()
movie1 = ("/home/pi/Videos/VID_chap1.mp4")
movie2 = ("/home/pi/Videos/VID_chap2.mp4")
movie3 = ("/home/pi/Videos/VID_chap3.mp4")

def vid1():
   os.system('killall omxplayer.bin')
   omxc = Popen(['omxplayer','--layer','2','--win','0,0,800,481', movie1])
   pid = subprocess.Popen([sys.executable,"01-Subprogram-serial.py"])
#   subprocess.popen.kill(pid) <This not works

def vid2():
   os.system('killall omxplayer.bin')
   omxc = Popen(['omxplayer','--layer','2','--win','0,0,800,481', movie2])
   pid = subprocess.Popen([sys.executable,"02-Subprogram-serial.py"])
#   subprocess.popen.kill(pid) <This not works

def vid3():
   os.system('killall omxplayer.bin')
   omxc = Popen(['omxplayer','--layer','2','--win','0,0,800,481', movie3])
   pid = subprocess.Popen([sys.executable,"03-Subprogram-serial.py"])
#   subprocess.popen.kill(pid) <This not works


B = Button(top, text ="Asean 1", command = vid1)
C = Button(top, text ="Asean 2", command = vid2)
D = Button(top, text ="Asean 3", command = vid3)


B.pack()
C.pack()
D.pack()

top.mainloop()

我想在开始新的子进程之前先杀死它

0 个答案:

没有答案