Python's threading.Timer
的時間精度取決於什麼?threading.Timer時間精度
是否依賴於使用的操作系統?
它取決於使用的Python實現嗎?
它取決於間隔?
如何進行基準測試?
那裏已經有基準了嗎?
Python's threading.Timer
的時間精度取決於什麼?threading.Timer時間精度
是否依賴於使用的操作系統?
它取決於使用的Python實現嗎?
它取決於間隔?
如何進行基準測試?
那裏已經有基準了嗎?
在CPython中,threading.Timer的準確性(waiting part)基於Condition.wait()調用。
Condition.wait()(implemented here)通過連續休眠完成,延遲時間爲min(delay * 2, remaining, .05)
,其中delay最初設置爲0.0005 # 500 us -> initial delay of 1 ms
。根據這個實現(這是OS獨立的),我們可以爭辯說,準確度至少是準確度。
但是,time.sleep()的準確性基於所使用的操作系統(here is the implementation : floatsleep()),在Windows上,它使用WaitForSingleObject()
和內部Windows計時器,在Linux上它使用select()
方法。
至少,因爲除了睡眠延遲,操作系統的電荷可能會干擾python進程的反應性,調度算法也會影響精度。