有没有办法拒绝预定的计时器事件?

时间:2016-09-09 12:35:24

标签: python multithreading

考虑一下。我想拒绝执行Timer事件:

import threading


def say_hello():
    print 'hello'

threading.Timer(10, say_hello, ()).start()
# now for some reason my plans have changed
# is there a way to erase that Timer and deny execution of say_hello?

1 个答案:

答案 0 :(得分:3)

t = threading.Timer(10, say_hello, ())
t.start()  # start timer
t.cancel()  # stop it