多处理python,两个进程

2024-09-27 02:24:24 发布

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

当检测到传感器1时,过程1被激活。 当Process1运行时检测到传感器2时,是否有方法使Process2运行?? 如果你能告诉我怎么做,我将不胜感激

from multiprocessing import Process, Queue, Manager

def p1(queue):
    for i in range(1000):
        print('p1 : %s' %i)
        time.sleep(0.5)
        
    print('p1 end') 
    
def p2(queue):
    for i in range(1000):
        print('p2 : %s' %i)
        time.sleep(0.5)
    print('p2 end')

if __name__ == "__main__":
    print('start')
    GPIO.add_event_detect(Sensor1, GPIO.RISING, bouncetime=800)
    GPIO.add_event_detect(Sensor2, GPIO.FALLING, bouncetime=1000)
    queue = Manager().Queue()
    
    while True:
        if GPIO.event_detected(Sensor1):
            #smooth(queue)
            process1 = Process(target = p1, args = (queue,))
            process1.start()
            process1.join()
            
        
        elif GPIO.event_detected(Sensor2):
            process2 = Process(target = p2, args=(queue,))
            process2.start()
            process2.join()

Tags: eventforgpioqueuedefmanager传感器process

热门问题