如何正确使用倒计时线程,如何提前停止?

时间:2015-02-11 16:56:29

标签: python multithreading

线程不按我预期的方式工作。

我有一个可行的解决方案,我可以通过Raspberry Pi和簧片开关监控冰箱的打开和关闭状态(声音播放暂停和暂停)。我现在想要在门打开太久时添加一个计时器来做某事。我想开始一个线程,它会在警报动作成为一个好主意之前睡眠x秒。当开关再次关闭时,我会用信号杀死线程。

我的方法失败了。 CountDown运行线程已启动,但终止信号命令已执行但无效。此外,不执行c.terminate()之后的命令。我查看了线程的示例,但它们似乎适用于更复杂的情况。我错过了什么?

代码:

#!/usr/bin/env python2.7

import threading, subprocess, sys, time, syslog
import RPi.GPIO as GPIO

sound = "/home/pi/sounds/fridge_link.mp3" # sound to play while switch is open
cmd = ['mplayer', '-nolirc', '-noconsolecontrols', '-slave', '-quiet', sound] # command to play sound
lim = 10 # seconds until warning

# thread for countdown (should be interruptable)
# based on http://chimera.labs.oreilly.com/books/1230000000393/ch12.html#_solution_197
class CountdownTask:
    def __init__(self):
        self._running = True

    def terminate(self):
        self._running = False
        print("thread killed")

    def run(self, n):
        print("start timer")
        time.sleep(n)
        ## action when timer isup 
        print("timer ended")


c = CountdownTask()
t = threading.Thread(target=c.run, args=(lim,))
t.daemon = True

p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
p.stdin.write('\npausing_keep pause\n')

REED = 27 # data pin of reed sensor (in)

# GPIO setup
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(REED,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

def edge(channel):
    if GPIO.input(REED):
        print("detect close")
        c.terminate()
        p.stdin.write('\npause\n')
        pass
    else:
        print("detect open")
        t.start()
    p.stdin.write('\npausing_toggle pause\n')

def main():
    GPIO.add_event_detect(REED, GPIO.BOTH,callback=edge,bouncetime=1000)
    while True:
        time.sleep(0.2)
        pass

#------------------------------------------------------------

if __name__ == "__main__": main()

新版本:

#!/usr/bin/env python2.7

import threading, subprocess, sys, time, syslog
import RPi.GPIO as GPIO

sound = "/home/pi/sounds/fridge_link.mp3" # sound to play while switch is open
cmd = ['mplayer', '-nolirc', '-noconsolecontrols', '-slave', '-quiet', sound] # command to play sound
lim = 10 # seconds until warning

# thread for countdown (should be interruptable)
class CountdownTask:
    global dooropen
    def __init__(self):
        self._running = True

    def terminate(self):
        self._running = False
        print("thread killed")

    def run(self, n):
      while self._running and dooropen == False:
          time.sleep(0.2)
          pass
      while self._running and dooropen:
        print("start timer")
        time.sleep(n)
        ## action when timer isup 
        print("timer ended")


c = CountdownTask()
t = threading.Thread(target=c.run, args=(lim,))
t.daemon = True

p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
p.stdin.write('\npausing_keep pause\n')

REED = 27 # data pin of reed sensor (in)

# GPIO setup
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(REED,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

dooropen = False # assuming door's closed when starting


def edge(channel):
    global dooropen
    if GPIO.input(REED): # * no longer reached
        if dooropen == False: # catch fridge compressor spike
            print("false close alert") 
            return
        p.stdin.write('\npause\n') 
        dooropen = False
        pass
    else:
        print("detect open")
        if dooropen == True:
            print("false open alert")
            return        
    p.stdin.write('\npausing_toggle pause\n')
    dooropen = True

def main():
    GPIO.add_event_detect(REED, GPIO.BOTH,callback=edge,bouncetime=1000)
    t.start()
    while True:
        time.sleep(0.2)
        pass

#------------------------------------------------------------

if __name__ == "__main__": main()

调整后的部分,现在正在工作:

    def run(self, n):
      while self._running and dooropen == False:
          time.sleep(0.2)
          pass
      while self._running and dooropen:
        time.sleep(n)
        if dooropen:
            ## action when timer isup 

1 个答案:

答案 0 :(得分:2)

您通过self._running编程的线程终止机制无效,因为您没有轮询/检查self._running方法中run()的状态(实际上是在你所指的例子。)

定期轮询会增加此处不必要的复杂性。您应该以不同的方式构建逻辑,这是简单而可靠的。示例代码:

import threading
import time


dooropen = True


def warnafter(timeout):
    time.sleep(timeout)
    if dooropen:
        print("Warning!")


t = threading.Thread(target=warnafter, args=(2,))
t.start()
time.sleep(1)
dooropen = False
t.join()

time.sleep(1)更改为time.sleep(3)并打印警告。为什么这样做,以及这如何转化为您的用例?

首先,让我们给出一些名字。你有你的主线程"警告线程"。这些是我的示例代码中架构的基石:

  • 在两个线程之间有一个共享状态,指示门是否打开,转换为是否应该发出警告的事实。我们将此状态称为dooropen,它可以是TrueFalse。它是一个可在两者中访问的变量,主线程的范围以及warn线程可以访问的范围。也就是说,它存在于共享内存中。

  • 这是您的惯例:dooropen仅从主线程写入。警告线程只读它。

  • 只要您认为合适的时间,就会生成警告线程。让它睡觉(确切的睡眠时间可能不可靠,特别是在嵌入式系统上)。

  • 关键部分:在警告线程中发出警报之前,让它检查dooropen状态。如果not dooropen,则不要发出警报!

你看到两种不同的范例吗?

你的范例是制造一个武装炸弹,计划在一定时间后爆炸。这枚炸弹不再与你交谈了。你的希望是,如果你不再需要爆炸,你可以在它爆炸之前化解/ 销毁炸弹。

我提议的范例是一种炸弹,实际上在它需要之前没有武装。在你的炸弹爆炸的时候,这个会问它是否真的应该这样做,然后才会自行武装并爆炸。

鉴于后一种范式,如果告知警告线程不执行其操作,则静默自行退出。 "从外部终止线程的概念"不需要!

在实践中,您需要一个更高级的概念,其中警告线程具有自己的 active开关。也就是说,您的主线程可以以受控方式停用单个警告线程。见这个例子:

import threading
import time


class WarnThread(threading.Thread):
    def __init__(self, timeout, name):
        threading.Thread.__init__(self)
        self._timeout = timeout
        self.active = True
        self.name = name
        self.start()

    def run(self):
        self._warnafter()

    def _warnafter(self):
        time.sleep(self._timeout)
        if self.active:
            print("WarnThread %s: Warning after timeout" % self.name)


ws = [WarnThread(2, i) for i in range(5)]

# Simulate spending some time doing other things,
# such as responding to external events.
time.sleep(1)

# Selectively deactivate some of the warn threads.
ws[0].active = False
ws[2].active = False

for w in ws:
    w.join()

输出:

WarnThread 4: Warning after timeout
WarnThread 1: Warning after timeout
WarnThread 3: Warning after timeout