在函数中取消计时器的问题(python)

时间:2019-05-12 15:42:53

标签: python

我有一个不断循环运行的函数,检查它是否应该启动或取消在函数范围内定义的计时器。计时器需要在函数中定义,因为回调也在函数中定义。我可以很好地启动计时器,但是当它尝试取消时,出现错误“分配前引用了本地变量'timer'”。

我尝试在全局范围内定义计时器及其回调(这是不希望的),但我遇到另一个错误“线程只能启动一次”。

import threading
import random

def start():
    trigger = random.randint(0,1)
    def do_something():
        print(trigger)
    if trigger == 0:
        timer = threading.Timer(2,do_something)
        timer.start()
    else:
        timer.cancel() #: if trigger is 1, I want to cancel the timer
    threading.Timer(1,start).start() #: start() is in a loop and is constantly checking trigger's value

start()

我希望根据触发器的值来启动或取消同一计时器。计时器及其回调应在函数中定义。

2 个答案:

答案 0 :(得分:0)

该程序显示了如何使用随机数来启动或停止计时器。

如果随机数连续选择0次,计时器将启动并允许继续计时直到时间用完并调用目标。

如果随机数选择1,则计时器将被取消,并且不会调用目标:

import threading
import random
import time

class Timing:
    def __init__(self):
        self.timer = None       # No timer at first
        self.something = None   # Nothing to print at first
        self.restart()

    def restart(self):
        self.run = threading.Timer(1.1, self.start)
        self.run.start()

    def cancel(self):
        if self.run is not None:
            self.run.cancel()
            self.run = None

    def start(self):
        trigger = random.randint(0, 1)
        self.do_start(trigger)

    def do_start(self, trigger):
        print('start', trigger)
        if trigger == 0:
            if self.timer is None:
                self.something = trigger
                self.timer = threading.Timer(2, self.do_something)
                self.timer.start()
        else:
            if self.timer is not None:
                self.timer.cancel()
                self.timer = None
                self.something=None
        self.restart()

    def do_something(self):
        print(self.something)

t = Timing()

print('sleeping...')
time.sleep(20)
t.cancel()
t.do_start(1)
t.cancel()
print('Done')

样本输出(ymmv是随机的)

sleeping...
start 1
start 0
start 1
start 0
start 0
0
start 1
start 0
start 1
start 1
start 1
start 1
start 1
start 0
start 1
start 0
start 0
0
start 1
start 0
start 1
Done

答案 1 :(得分:0)

我已经从@quamrana和@smci学习到并想到了

import threading
import random

class Timer():
    pass
t = Timer()

def start():
    trigger = random.randint(0,1)
    def do_something():
        print(trigger)
    if trigger == 0:
        t.timer = threading.Timer(1,do_something)
        t.timer.start()
    else:
        if hasattr(t,'timer'):
            t.timer.cancel() 
    threading.Timer(1,start).start() 

start()

这似乎在解决问题的同时又使代码紧凑。