繁体   English   中英

Python 多处理两次处理

[英]Python multiprocessing twice process

我尝试使用 2 个进程,但我收到一条错误消息,提示“无法两次启动进程”。 问题是什么?

我正在编写一个在 Process1 运行时也可以运行 Process2 的代码。

import cv2
import cv2 as cv
import numpy as np
import io
from PIL import Image
import time
import picamera
import picamera.array
import RPi.GPIO as GPIO
from picamera.array import PiRGBArray
from multiprocessing import Process, Queue, Manager

Sensor1 = 18
Sensor2 = 23 

# Initialize the GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(Sensor1, GPIO.IN, GPIO.PUD_DOWN)
GPIO.setup(Sensor2, GPIO.IN, GPIO.PUD_DOWN)


def f1():
    print("funtion 1")
    
def f2():  
    print("funtion 2")

if __name__ == "__main__":
    print('start')
 
    GPIO.add_event_detect(Sensor1, GPIO.RISING, bouncetime=800)
    GPIO.add_event_detect(Sensor2, GPIO.FALLING, bouncetime=1000)

    process1 = Process(target=f1, args=())
    process2 = Process(target=f2, args=())

    while True:
        if GPIO.event_detected(Sensor1):
            print("camera Sensor detect")
            process1.start()

        elif GPIO.event_detected(Sensor2):
            print("cylinder Sensor detect")
            process2.start()

解决方案已经在@jasonharper 评论中,但您似乎仍然不理解它 - 所以我放了这段代码。

您必须while True内创建Process()

while True:
    if GPIO.event_detected(Sensor1):
        print("camera Sensor detect")
        process1 = Process(target=f1, args=())
        process1.start()

    elif GPIO.event_detected(Sensor2):
        print("cylinder Sensor detect")
        process2 = Process(target=f2, args=())
        process2.start()

暂无
暂无

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

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