我想在运行test.py后运行strandtest.py,持续5秒。当我从遥控器获得输入时,strandtest.py必须停止,这样我就可以再次进入菜单。它就像是Windows中的屏幕保护程序。我可以在5秒后开始一个子进程。但是当它运行时我无法阻止它。我试过p.kill()和p.terminate(),但它们都不起作用。
os.killpg(os.getpgid(p.pid),signal.SIGTERM)
给出
模块'对象没有属性'pid'
你能帮助我吗?
这是我的代码
import lirc
import time
import os
import signal
from subprocess import PIPE,Popen
import subprocess
from neopixel import *
Volume = 60
mute = False
bool = False
bool2 = False
LEDS = 100
PIN = 18
BRIGHTNESS = 255
R = 255
G = 255
B = 255
LED_CHANNEL = 0
LED_STRIP = ws.WS2811_STRIP_GRB
sockid=lirc.init("lifetec", blocking = False)
strip = Adafruit_NeoPixel(10, PIN, 1050000, 10, False, BRIGHTNESS, LED_CHANNEL, LED_STRIP)
strip.begin()
def VolumeUp(procent, Bool):
if Bool == False:
if procent < 100:
Procent = procent + 4
os.system('amixer -c 1 set Speaker ' + str(Procent) + '%')
os.system('aplay /share/Wav/Menu/beep-22.wav')
return Procent
else:
os.system('amixer -c 1 set Speaker ' + str(procent) + '%')
os.system('aplay /share/Wav/Menu/button-3.wav')
return procent
def VolumeDown(procent, Bool):
if Bool == False:
if procent > 0:
Procent = procent - 4
os.system('amixer -c 1 set Speaker ' + str(Procent) + '%')
os.system('aplay /share/Wav/Menu/beep-22.wav')
return Procent
return procent
def Mute(Bool):
print(str(Bool))
os.system('aplay /share/Wav/Menu/beep-22.wav')
os.system('amixer sset Speaker toggle')
os.system('aplay /share/Wav/Menu/beep-23.wav')
if Bool == False:
Bool = True
else:
Bool = False
print(str(Bool))
return Bool
def wheel(pos):
"""Generate rainbow colors across 0-255 positions."""
if pos < 85:
return Color(pos * 3, 255 - pos * 3, 0)
elif pos < 170:
pos -= 85
return Color(255 - pos * 3, 0, pos * 3)
else:
pos -= 170
return Color(0, pos * 3, 255 - pos * 3)
def rainbow(strip, wait_ms=20, iterations=1):
for j in range(256*iterations):
print("JJJJ = " + str(j))
for i in range(strip.numPixels()):
strip.setPixelColor(i, wheel((i+j) & 255))
strip.show()
#time.sleep(0.01)
Gettime = True
pause = False
while True:
if Gettime == True:
timenow = time.time()
print("timenow Gettime")
print(timenow)
Gettime = False
if time.time() - timenow >= 5:
if pause == False:
print time.time() - timenow
print("pause")
bool = True
pause = True
if bool == True:
p = subprocess.Popen('python strandtest.py', stdout=subprocess.PIPE, shell=True, preexec_fn=os.setsid)
bool = False
if bool2 == True:
gelukt = os.killpg(os.getpgid(p.pid), signal.SIGTERM)
Gettime = True
print(gelukt)
bool2 = False
codeIR = lirc.nextcode()
if codeIR != []:
if codeIR[0] == "KEY_VOLUMEUP":
print codeIR[0]
print Volume
Volume = VolumeUp(Volume, mute)
print Volume
if codeIR[0] == "KEY_VOLUMEDOWN":
print codeIR[0]
print Volume
Volume = VolumeDown(Volume, mute)
print Volume
if codeIR[0] == "KEY_0":
print codeIR[0]
print Volume
mute = Mute(mute)
print Volume
if codeIR[0] == "KEY_POWER":
print codeIR[0]
os.system("/share/RadVanAdvontuur.py 100 255 255 0")
if codeIR[0] == "KEY_PAGEUP":
print codeIR[0]
bool2 = True
pause = False
if codeIR[0] == "KEY_PAGEDOWN":
print codeIR[0]
bool = True
print "script running"
答案 0 :(得分:0)
如果直接在没有shell的情况下运行它,如下所示:
p = subprocess.Popen('python strandtest.py', stdout=subprocess.PIPE, preexec_fn=os.setsid)
然后您可以使用p.kill()或p.terminate()来结束该过程。