这是我的课程,用于开启,关闭和覆盖树莓中的LED闪烁 我想在一段时间内停止线程闪烁... 但线程不会在后台运行......
类LED:
_GPIOPORT=None flagstop=0 flag=threading.Event() def __init__(self,GPIONUM): self._GPIOPORT=GPIONUM GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) GPIO.setup(self._GPIOPORT, GPIO.OUT) self.flag.set() def TurnOn(self): self.flag.clear() print 'Turn On ...' GPIO.output(self._GPIOPORT,True) def TurnOff(self): self.flag.clear() print 'Turn Off ...' GPIO.output(self._GPIOPORT,False) def Blink(self,delay,e): print 'Thread Blink Create ...' t1=threading.Thread(self.BlinkThread(delay,e)) t1.start() print 'Thread Started' def BlinkThread(self,delaytime,event): print 'BlinkTrehad Start ....' while not event: GPIO.output(self._GPIOPORT,False) sleep(delaytime) GPIO.output(self._GPIOPORT,True) sleep(delaytime)
这是我使用此课程的模块:
import LED
from time import sleep
import threading
if __name__ == '__main__':
e=threading.Event()
e.set()
ledblue=LED.LED(11)
ledblue.TurnOn()
sleep(1)
ledblue.Blink(1,e)
ledblue.TurnOn()
从不在“线程启动”中打印consol并在行中循环运行
答案 0 :(得分:0)
您在Thread构造函数中调用BlinkThread()而不是仅传递函数引用和参数,并让线程启动该函数。将其更改为:
def Blink(self,delay,e):
print 'Thread Blink Create ...'
t1=threading.Thread(target=self.BlinkThread, args=(delay,e))
t1.start()
print 'Thread Started'