Python2.7.9在运行scrip时不能终止子进程

2024-10-02 00:21:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我想跑strandtest.py运行后测试.py5秒。当我收到遥控器的输入信号时strandtest.py我得停下来,这样我就可以继续看菜单了。它就像windows中的屏幕保护程序。我可以在5秒后启动一个子进程。但当它跑的时候我无法阻止它。我尝试过p.kill()和p.terminate(),但它们都不起作用。在

基洛斯PG(操作系统.getpgid(p.pid),signal.SIGTERM信号)

给予

module'object没有属性'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"

Tags: posimportfalsetruereturniftimeos
1条回答
网友
1楼 · 发布于 2024-10-02 00:21:27

如果不使用shell直接运行它,如下所示:

p = subprocess.Popen('python strandtest.py', stdout=subprocess.PIPE, preexec_fn=os.setsid)

然后可以使用p.kill()或p.terminate()来结束进程。在

相关问题 更多 >

    热门问题