2015-01-07 1 views
1

내가하고있는 일은 웹 사이트의 새로운 내용을 확인하는 것입니다. threading.Timer는 50 초마다 새 내용을 확인합니다. 새 콘텐츠를 발견 한 경우 1 시간 동안 해당 기능을 일시 중지하고 싶습니다.일시 중지 threading.Tamer for 1hr

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(); 
+1

['time.sleep()'] (https://docs.python.org/2/library/time.html#time.sleep)? – NPE

+0

@ NPE 나는 그것을 시험해 보았다. 멈추거나 멈추지 않습니다. –

답변

0

가장 간단한 방법은 당신이 다시 함수를 호출하기 전에 대기 할 시간을 알 때까지 타이머를 다시 시작하지 아마도 : 그 어떤 이유로 할 수없는 경우

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 문이나 좀 더 복잡한 논리를 사용해야 할 수도 있습니다.

+0

고마워, 그게 효과가 :) –

관련 문제