python 定時器 timer QTimer


需求背景:

一秒記錄一個數據,數據從儀器讀取,經過一系列的操作后記錄到csv文件(該過程從發命令到成功寫入CSV需要的操作時間不等,從幾毫秒到幾百毫秒),記錄時間一天左右!

==========================================================================

第一次實現 使用 time.sleep()

 1 import time  
 2 import datetime
 3 
 4 def hello():
 5     now = datetime.datetime.now()
 6     print(str(now))
 7     fh = open("test.csv",'a')
 8     fh.write(str(now))
 9     fh.write('\n')
10     fh.flush()
11     time.sleep(0.1) #模擬實際代碼執行時間
13 if __name__ == "__main__": 
14   while True:
15     hello()
16     time.sleep(1)

結果:

 從上圖中看出,這不合要求,少了15:37:04 時刻的數據,因為每次循環,代碼執行的時間都在累加,導致實際記錄間隔是 1s+代碼執行時間。

 

第二次實現  python Timer 定時器

補充Timer知識點

class threading.Timer(intervalfunctionargs=[]kwargs={})

Create a timer that will run function with arguments args and keyword arguments kwargs, after interval seconds have passed.

For example:

def hello(): print "hello, world" t = Timer(30.0, hello) t.start() # after 30 seconds, "hello, world" will be printed 

通過測試這里最大的問題是,hello()只執行一次,但實際要的效果是每隔1s連續不斷的執行hello(),於是通過百度找到下面方法:

import threading
import time  
import datetime

def hello(uut_dev,cmd):
    now = datetime.datetime.now()
    print(str(now))
    fh = open("test.csv",'a')
    fh.write(str(now))
    fh.write('\n')
    fh.flush()
    time.sleep(0.2) #模擬實際代碼執行時間
    t1 = threading.Timer(1, hello,("uut_dev","FETCH?\r\n"))
    t1.start()

if __name__ == "__main__":
    t1 = threading.Timer(1, hello,("uut_dev","FETCH?\r\n"))
    t1.start()

代碼執行結果:

 很明顯又是一次不合格的數據,於是又想了下面的方法:

 1 import threading
 2 import time  
 3 import datetime
 4 
 5 def hello(uut_dev,cmd):
 6     now = datetime.datetime.now()
 7     print(str(now))
 8     fh = open("test.csv",'a')
 9     fh.write(str(now))
10     fh.write('\n')
11     fh.flush()
12     time.sleep(0.2)
13 
14 if __name__ == "__main__":
15     while True:
16         t1 = threading.Timer(0, hello,("uut_dev","FETCH?\r\n"))
17         t1.start()
18         time.sleep(1)

執行結果:

 

 從結果看出,效果好了很多,但每次執行還是多了大概3ms,很明顯要是執行的時間足夠長肯定還是會有記錄誤差

第三次實現  pyqt QTimer 定時器

 1 import sys
 2 import datetime
 3 import time
 4 from PyQt4.QtGui import *
 5 from PyQt4.QtCore import *
 6 
 7 def hello():
 8     now = datetime.datetime.now()
 9     print(str(now))
10     fh = open("test.csv",'a')
11     fh.write(str(now))
12     fh.write('\n')
13     fh.flush()
14     time.sleep(0.2)
15 
16 if __name__ == "__main__":
17     app = QApplication(sys.argv)
18     timer = QTimer()
19     QObject.connect(timer,SIGNAL("timeout()"), hello)
20     timer.start(1000)
21     sys.exit(app.exec_())

執行結果:

 

 好像就是需要的效果,雖然每次執行有幾毫秒的誤差,但能保證長時間的1s一次記錄


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM