python - 将变量传递给进程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()
解决方案
推荐阅读
- php - PHPExcel 意外的格式化数字
- vxworks - 在 VMware 上运行 VxWorks
- bash - 按 bash 中的所有字段进行数字排序
- vue.js - 如何从 vue-cli 中数组的 v-for 循环中仅更新选定值的数据
- reporting-services - 带有多参数文本的 SSRS 过滤器
- c - 我应该在哪里设置局部变量以提高效率?
- jquery - 在 jquery ui 中拖放后单击对象
- angular - 我在 Angular 的最新版本中使用 Mat Autocomplete,通常在我的项目中使用它,出现此类错误
- sql - 如何告诉“union all”实际选择哪个表?
- java - Spring Boot - log4j2.properties 创建日志文件但不将日志写入文件