Python 暂停线程。计时器1小时

Python 暂停线程。计时器1小时,python,multithreading,Python,Multithreading,我正在做的是检查网站上的新内容。计时器每50秒检查一次新内容。如果发现新内容,我希望它暂停功能1小时 def examplebdc (): threading.Timer(50.00, examplebdc).start (); #content id wordv = 'asdfsdfm' if any("m" in s for s in wordv): print("new post") #pause this threading.Timer (or function) fo

我正在做的是检查网站上的新内容。计时器每50秒检查一次新内容。如果发现新内容,我希望它暂停功能1小时

def examplebdc ():
threading.Timer(50.00, examplebdc).start ();
#content id
wordv = 'asdfsdfm'

if any("m" in s for s in wordv):
    print("new post")
    #pause this threading.Timer (or function) for 1hr. 
examplebdc();

最简单的方法可能是在知道再次调用函数之前要等待多长时间后才重新启动计时器:

def examplebdc():
    wordv = 'asdfsdfm'

    if any("m" in s for s in wordv):
        print("new post")
        threading.Timer(60*60, examplebdc).start()
    else:
        threading.Timer(50, examplebdc).start()

examplebdc()
如果由于某种原因无法执行此操作,您可以更改创建和启动计时器的方式,以便以后可以引用并取消计时器:

def examplebdc():
    # lets assume we need to set up the 50 second timer immediately
    timer = threading.Timer(50, examplebdc)   # save a reference to the Timer object
    timer.start()                             # start it with a separate statement

    wordv = 'asdfsdfm'

    if any("m" in s for s in wordv):
        print("new post")
        timer.cancel()                        # cancel the previous timer
        threading.Timer(60*60, examplebdc).start()   # maybe save this timer too?

examplebdc()

在单个函数中,这很简单,只需使用一个变量即可。如果您的计时器是在其他地方启动的,您可能需要使用一个或多个
global
语句或其他更复杂的逻辑来传递计时器引用。

?@NPE我试过了。它不会停止或暂停。