Python 如何显示一个类中连续计算的函数值?
我希望程序连续运行,并在管脚上检测到信号梯度后立即从方法(func1-func4)输出值 这只是一个测试代码,用来理解我如何访问这些值,因为我写了一个“程序”,它的表面显示了一台机器的不间断值,我只使用全局变量,我不想在这里发布,因为这很尴尬。。。 我想去掉所有的全局变量 一个树莓pi连接到机器,我用它通过gpio截获4个信号Python 如何显示一个类中连续计算的函数值?,python,multithreading,Python,Multithreading,我希望程序连续运行,并在管脚上检测到信号梯度后立即从方法(func1-func4)输出值 这只是一个测试代码,用来理解我如何访问这些值,因为我写了一个“程序”,它的表面显示了一台机器的不间断值,我只使用全局变量,我不想在这里发布,因为这很尴尬。。。 我想去掉所有的全局变量 一个树莓pi连接到机器,我用它通过gpio截获4个信号 cavity = 2 count = 0 scrap = 0 uptime = datetime.timedelta(0) downtime = datetime.tim
cavity = 2
count = 0
scrap = 0
uptime = datetime.timedelta(0)
downtime = datetime.timedelta(0)
def func1(channel):
if GPIO.input(7) == 0:
while True:
uptime = uptime + datetime.timedelta(0,1)
time.sleep(1)
if GPIO.input(7) == 1 or GPIO.input(37) == 0:
break
def func2(channel):
scrap = scrap + cavity
def func3(channel):
count = count + cavity
def func4(channel):
if GPIO.input(37) == 0:
while True:
downtime = downtime + datetime.timedelta(0,1)
if GPIO.inpu(37) == 1:
break
GPIO.add_event_detect(7, GPIO.RISING, callback = func1, bouncetime = 100) #Run
GPIO.add_event_detect(29, GPIO.RISING, callback = func2, bouncetime = 100) #Scrap
GPIO.add_event_detect(13, GPIO.RISING, callback = func3, bouncetime = 100) #Count
GPIO.add_event_detect(37, GPIO.RISING, callback = func4, bouncetime = 100) #Alarm
class Output(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.show()
def show(self):
print('ich werde aufgerufen')
while True:
print(uptime)
print(scrap)
print(count)
print(downtime)
print('############################')
time.sleep(5)
thread2 = Output()
thread2.start()
只输出零这是意料之中的,但我如何访问变量,以便在信号检测时值增加您的函数只是创建自己的局部变量,因此全局变量不会改变 试试这个:
def func1(channel):
global uptime
if GPIO.input(7) == 0:
while True:
uptime = uptime + datetime.timedelta(0,1)
time.sleep(1)
if GPIO.input(7) == 1 or GPIO.input(37) == 0:
break
def func2(channel):
global scrap
scrap = scrap + cavity
def func3(channel):
global count
count = count + cavity
def func4(channel):
global downtime
if GPIO.input(37) == 0:
while True:
downtime = downtime + datetime.timedelta(0,1)
if GPIO.inpu(37) == 1:
break
您的代码编写为两个类:
class Events:
def __init__(self):
GPIO.add_event_detect(7, GPIO.RISING, callback = self.func1, bouncetime = 100) #Run
GPIO.add_event_detect(29, GPIO.RISING, callback = self.func2, bouncetime = 100) #Scrap
GPIO.add_event_detect(13, GPIO.RISING, callback = self.func3, bouncetime = 100) #Count
GPIO.add_event_detect(37, GPIO.RISING, callback = self.func4, bouncetime = 100) #Alarm
self.cavity = 2
self.count = 0
self.scrap = 0
self.uptime = datetime.timedelta(0)
self.downtime = datetime.timedelta(0)
def func1(self, channel):
if GPIO.input(7) == 0:
while True:
self.uptime = self.uptime + datetime.timedelta(0,1)
time.sleep(1)
if GPIO.input(7) == 1 or GPIO.input(37) == 0:
break
def func2(self, channel):
self.scrap = self.scrap + cavity
def func3(self, channel):
self.count = self.count + cavity
def func4(self, channel):
if GPIO.input(37) == 0:
while True:
self.downtime = self.downtime + datetime.timedelta(0,1)
if GPIO.inpu(37) == 1:
break
class Output(threading.Thread):
def __init__(self, events):
threading.Thread.__init__(self)
self.events = events
self.show()
def show(self):
print('ich werde aufgerufen')
while True:
print(self.events.uptime)
print(self.events.scrap)
print(self.events.count)
print(self.events.downtime)
print('############################')
time.sleep(5)
events = Events()
thread2 = Output(events)
thread2.start()
您的每个函数都需要将它们写入的变量声明为
global
?我经常阅读不应该使用的全局变量。我知道我可以用global解决这个问题,但我也可以用其他方法解决它吗?是的,制作另一个类,它将上述所有内容结合起来类输出
。你能给我举个例子吗?