简体   繁体   English

如何在Python中停止一个进程

[英]how to stop one process from another in Python

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? 在Raspberry Pi上,我正在运行两个循环,这些进程不断循环,直到触发一个输入停止其中一个进程但是如何停止这两个进程? The only thing that works is pressing control+c in the console I can't get it to stop with code at this moment. 唯一有效的是在控制台中按下control + c我现在无法用代码停止它。

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)

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. 上面的代码只是按下按钮,按下按钮后它会触发stopBlink功能,闪烁红色LED 20次。

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)

The above code simply flashes a blue LED on and off in a loop. 上面的代码只是在一个循环中打开和关闭蓝色LED。

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

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. 现在,当我点击stopBlink函数时,我希望它停止所有其他正在运行的进程,但我似乎无法让它停止testRunning函数。 I've tried adding 我试过添加

sys.exit(1)

To the stopBlink function but it has no affect on the other functions running. 到stopBlink函数但它对运行的其他函数没有影响。

You could use multiprocessing.Event for interprocess synchronization. 您可以使用multiprocessing.Event进行进程间同步。

Here is a similar example: 这是一个类似的例子:

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()

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. 我不知道Raspberry Pi是否有特殊要求,但这是你如何以更通用的方式处理python线程和进程。

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

相关问题 在 PYTHON 中将网格从一个进程转移到另一个进程 - Transferring a mesh from one process to another in PYTHON 如何停止 python 以完成一个操作的执行然后启动其他进程 - How to STOP python to complete execution on one operation and then start other process Python:一个进程如何等待另一个进程创建fifo? - Python: How can one process wait for a fifo to be created by another process? 如何将异常从一个进程传递到另一个进程? - How to pass exception from one process to another? 如何从一个过程向另一个过程发出信号? - How to signal from one process to another? 如何阻止多个进程创建另一个进程的多个实例? - How to stop multiple processes from creating multiple instances of another process? 如何阻止一个网格元素的位置由于更新python tkinter中另一个网格元素的内容而改变? - How to stop the location of one grid element from changing due to the update of the content of another grid element in python tkinter? Python:如何从另一个进程读取变量? - Python: how to read variables from another process? 如何使用Python将文件从一个子文件夹处理到每个目录中的另一个子文件夹? - How to process files from one subfolder to another in each directory using Python? 如何从Tkinter窗口立即停止Python进程? - How do I stop a Python process instantly from a Tkinter window?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM