简体   繁体   English

Python 2.7.9在运行脚本时无法终止子进程

[英]Python 2.7.9 Can not kill subprocess while running script

I want to run strandtest.py after running test.py for 5 sec. 我想在运行test.py 5秒后再运行strandtest.py。 When I get an input from my remote strandtest.py has to stop so I can go on in the menu again. 当我从远程strandtest.py获得输入时,必须停止,这样我才能再次进入菜单。 it's like a screensaver in windows. 就像Windows中的屏幕保护程序。 I can start a subprocess after 5 sec. 我可以在5秒钟后开始一个子过程。 but when it runs i can't stop it. 但是当它运行时我无法停止它。 I tried p.kill() and p.terminate() but they both won't work. 我试过了p.kill()和p.terminate(),但是它们都不起作用。

os.killpg(os.getpgid(p.pid), signal.SIGTERM) os.killpg(os.getpgid(p.pid),signal.SIGTERM)

gives

module' object has no attribute 'pid' 模块”对象没有属性“ pid”

Can you help me? 你能帮助我吗?

Here's my code 这是我的代码

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"

If you run it directly without a shell, like this: 如果直接在没有外壳的情况下运行它,如下所示:

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

then you can use p.kill() or p.terminate() to end the process. 那么您可以使用p.kill()或p.terminate()结束该过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM