首页 > 解决方案 > 将变量传递给进程python

问题描述

需要有关如何修改/修复代码的帮助,以使我能够控制流程中发生的事情。我环顾四周并阅读我需要创建一个进程可以读取的全局变量或使用事件函数来触发进程。但问题是我不知道如何在类函数中实现它们。我认为如果我遵循pyimagesearch 代码,它会起作用,但它似乎只适用于线程模块,而不适用于多处理模块。

import RPi.GPIO as GPIO
from RPI.GPIO import LOW,OUT,HIGH,BCM
import multiprocessing as mp
import time

class TestClass():
    def __init__(self,PinOne=22,PinTwo=27):
        self.PinOne = PinOne
        self.PinTwo = PinTwo

        self.RunningSys = True

        GPIO.setmode(BCM)
        GPIO.setup(PinOne,OUT)
        GPIO.output(PinOne,LOW)
        GPIO.setup(PinTwo,OUT)
        GPIO.output(PinTwo,LOW)

   def Testloop(self):
        while self.RunningSys:
            GPIO.output(PinOne,HIGH)
            GPIO.output(PinTwo,HIGH)
            time.sleep(1)
            GPIO.output(PinOne,LOW)
            GPIO.output(PinTwo,LOW)
        GPIO.output(PinOne,LOW)
        GPIO.output(PinTwo,LOW)

   def StopPr(self):
        self.RunningSys = False

   def MProc(self):
       MPGP = mp.process(target=TestClass().Testloop())
       MPGP.start()
       MPGP.join()

在单独的脚本中

From testfile import TestClass
import time

TestClass().MProc()
time.sleep(4)
TestClass().StopPr()

标签: pythonraspberry-pi

解决方案


推荐阅读