2012-06-20 123 views
3
def On_Instrumentation_StartAnimation(): 

    """ 
    Syntax  : On_Instrumentation_StartAnimation() 
    Purpose  : Fired if the animation is started 
    Parameters : None 
    """ 
    print "----------------------------------------------------------------------------------------" 
    localtime = time.asctime(time.localtime(time.time())) 
    global start 
    start = time.clock() 
    print "The user entered Animation Mode at local time: ", localtime 
    print("This data has also been written to 'c:\dSPACE71\cdlog\cdlog.txt'") 
    threading.Timer(2, ExecuteDemo).start() 
    ExecuteDemo() 
    # Printing to the text file 
    file1 = open('c:\dSPACE71\cdlog\cdlog.txt', 'a') 
    file1.write("\n----------------------------------------------------------------------------------------") 
    file1.write("\nThe user entered Animation Mode at local time: ") 
    file1.write(localtime) 
    file1.close()  

def ExecuteDemo() 
    . 
    . 
    . 
    Current_value = Current.Read() 
    localtime = time.asctime(time.localtime(time.time())) 
    print "The current reading at localtime:", localtime, "is", str(Current_value) + "." 
    # Printing to the text file 
    file1 = open('c:\dSPACE71\cdlog\cdlog.txt', 'a') 
    file1.write("\n----------------------------------------------------------------------------------------") 
    file1.write("\nThe current reading at localtime: ") 
    file1.write(localtime) 
    file1.write(" is: ") 
    file1.write(str(Current_value)) 
    file1.close() 
    . 
    . 
    . 

正如你可以看到的,我試圖在調用StartAnimation函數後每2秒重複一次ExecuteDemo()函數。但我的問題在於,我的ExecuteDemo()只運行兩次。我怎樣才能讓它繼續重複?我錯過了什麼嗎?Python threading.Timer只重複一次

回答

11

從文檔:

類threading.Timer

,一個指定的時間間隔已經過去之後執行的功能的線程。

這意味着Threading.Timer將調用一個函數在指定的時間段之後。正如你注意到的那樣,它只會被調用一次。這裏的解決方案將在ExecuteDemo(..)函數結束時再次設置定時器。

def ExecuteDemo(): 
    . 
    . 
    . 
    threading.Timer(2, ExecuteDemo).start() 

在我看來,上述方法是有點低效率。就像每2秒創建一個新線程,並且一旦它執行該函數,它在創建下一個線程之前就會死亡。

我建議是這樣的:

def ExecuteDemoCaller(): 
    #while True: # or something.. 
    while someCondition: 
     ExecuteDemo() 
     time.sleep(2) 
+1

+1了'而真:'方法。在我看來,這更像Pythonic,並且更多地表達意圖,即無限循環。 'while breakCondition:'也是有意義的,假設ExecuteDemo可以改變存儲在那裏的值。 –

+0

@ g.d.d.c爲什麼我在那裏有'#或什麼東西:) – SuperSaiyan

+0

+1是出於同樣的原因。然而,值得指出的是,所有給出的解決方案都有相同的問題:平均不會達到30次/分鐘,總是會變慢。唯一的解決方法是使用sleep-until函數,時間戳總是增加2秒。 – abarnert

1

docs

類threading.Timer(間隔功能ARGS = [],kwargs = {})¶ 創建將運行一個計時器功能帶參數參數和關鍵字 參數kwargs,之後間隔秒已過。

我無法找到任何關於重複調用的內容。也許別人有更好的答案,或者你必須做手動處理(這不會太難)。

0

定時器不重複。您可以在回調函數結束時設置另一個計時器。

0
class setInterval(threading.Thread): 
def __init__(self, interval, function, args=[], kwargs={}): 
    threading.Thread.__init__(self) 
    self.interval = interval 
    self.function = function 
    self.args = args 
    self.kwargs = kwargs 
    self.finished = threading.Event() 
    self.flag_run = True 
def cancel(self): 
    self.finished.set() 
def run(self): 
    while self.flag_run: 
    self.finished.wait(self.interval) 
    if not self.finished.is_set(): self.function(*self.args, **self.kwargs) 
    else: self.flag_run = False 
    self.finished.set() 

十分鐘閱讀後 「threading.py」 我用這個代碼