如何在 Python 中停止一个进程与另一个进程 [英] how to stop one process from another in Python

查看:103
本文介绍了如何在 Python 中停止一个进程与另一个进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Raspberry Pi 上,我正在运行两个不断循环的进程,直到触发一个停止其中一个进程的输入,但如何停止这两个进程?唯一有效的方法是在控制台中按 control+c 我现在无法通过代码停止.

On a Raspberry Pi I'm running two processes which loop constantly until an input is triggered which stops one of the processes but how do I stop both processes? The only thing that works is pressing control+c in the console I can't get it to stop with code at this moment.

def stopButton():
while 1:
    wiringpi2.pinMode(52,0) 
    stopBut = wiringpi2.digitalRead(52)
    print (stopBut)
    wiringpi2.delay(500)
    if (stopBut == 0):
        stopBlink()

def stopBlink():
    redBlink = int(0)
    while (redBlink < int(20)):
        wiringpi2.digitalWrite(pin2,HIGH) 
        wiringpi2.delay(50)
        wiringpi2.digitalWrite(pin2,LOW)  
        wiringpi2.delay(50)
        redBlink += int(1)

上面的代码只是寻找一个按钮按下,一旦按下按钮,它就会触发 stopBlink 功能,红色 LED 闪烁 20 次.

The above code simply looks for a button press, once the button is pressed it triggers to stopBlink function which flashes a red LED 20times.

def testRunning():
while 1:
    wiringpi2.digitalWrite(pin3,HIGH) # Write HIGH to pin 2(U14 pin 2)
    wiringpi2.delay(1000)
    wiringpi2.digitalWrite(pin3,LOW)  # Write LOW to pin
    wiringpi2.delay(1000)

上面的代码只是简单地循环闪烁蓝色 LED.

The above code simply flashes a blue LED on and off in a loop.

if __name__ == '__main__':
try:
    P1 = Process(target = stopButton)
    P2 = Process(target = testRunning)
    P1.start()
    P2.start()
    P1.join()
    P2.join()

现在,当我点击 stopBlink 函数时,我希望它停止所有其他正在运行的进程,但我似乎无法让它停止 testRunning 函数.我试过添加

Now when I hit the stopBlink function I want it to stop all other running processes but I just can't seem to get it to stop the testRunning function. I've tried adding

sys.exit(1)

到了stopBlink函数,但是对其他函数的运行没有影响.

To the stopBlink function but it has no affect on the other functions running.

推荐答案

您可以使用 multiprocessing.Event 进行进程间同步.

You could use multiprocessing.Event for interprocess synchronization.

这是一个类似的例子:

from multiprocessing import Process, Event, Lock
from time import sleep

def wait(stop_event, lock):
    with lock:
        print "Waiting 2 s..."
    sleep(2)
    with lock:
        print "Stopping processes..."
    stop_event.set()

def loop(stop_event, lock):
    while not stop_event.is_set():
        with lock:
            print "Looping every 0.5 s..."
        sleep(0.5)

if __name__ == '__main__':
    stop_event, lock = Event(), Lock()
    wait_process = Process(target=wait, args=(stop_event, lock))
    loop_process = Process(target=loop, args=(stop_event, lock))
    wait_process.start(), loop_process.start()
    wait_process.join(), loop_process.join()

我不知道 Raspberry Pi 是否有特殊要求,但这是您以更通用的方式处理 Python 线程和进程的方式.

I don't know if the Raspberry Pi has special requirements, but this is how you deal with python threads and processes in a more generic way.

这篇关于如何在 Python 中停止一个进程与另一个进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆