簡體   English   中英

Python threading.Timer 只重復一次

[英]Python threading.Timer only repeats once

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() 只運行了兩次。 我怎樣才能讓它不斷重復? 我錯過了什么嗎?

從文檔:

類 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)

文檔

class threading.Timer( interval , function , args =[], kwargs ={})¶ 創建一個計時器,在間隔秒過去后,該計時器將運行帶有參數args和關鍵字參數kwargs 的函數

我找不到任何關於重復調用某些東西的信息。 也許其他人有更好的答案,或者您必須親自動手(這不會太難)。

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

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” 10 分鍾后,我使用此代碼

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()只運行兩次。 我怎樣才能使它不斷重復? 我想念什么嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM