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只重複一次
+1了'而真:'方法。在我看來,這更像Pythonic,並且更多地表達意圖,即無限循環。 'while breakCondition:'也是有意義的,假設ExecuteDemo可以改變存儲在那裏的值。 –
@ g.d.d.c爲什麼我在那裏有'#或什麼東西:) – SuperSaiyan
+1是出於同樣的原因。然而,值得指出的是,所有給出的解決方案都有相同的問題:平均不會達到30次/分鐘,總是會變慢。唯一的解決方法是使用sleep-until函數,時間戳總是增加2秒。 – abarnert