线程.定时器时间精度
Python的threading.Timer
的时间精度是多少 依赖?
这取决于所使用的操作系统吗?
它取决于所使用的 Python 实现吗?
与间隔有关吗?
如何对其进行基准测试?
已经有基准了吗?
What does the time accuracy of Python's threading.Timer
depend on?
Does it depend on the OS used?
Does it depend on the Python implementation used?
Does it depend on the interval?
How can it be benchmarked?
Are there already benchmarks out there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在CPython中,threading.Timer精度(等待部分 ) 基于 Condition.wait() 调用。
Condition.wait()(此处实现)是通过延迟为
min(delay * 2, left, .05)
的连续睡眠来完成,其中延迟最初设置为0.0005 # 500 us ->初始延迟 1 毫秒
。根据这个实现(即与操作系统无关),我们可以认为精度至少是 time.sleep 精度。但是, time.sleep() 的准确性取决于所使用的操作系统(这里是实现:floatsleep()),在Windows上,它使用
WaitForSingleObject()
使用内部 Windows 计时器,在 Linux 上它使用select()
方法。至少,由于除了睡眠延迟之外,操作系统的负载可能会干扰Python进程的反应性,因此调度算法也会影响准确性。
In CPython, the threading.Timer accuracy (waiting part) is based on a Condition.wait() call.
The Condition.wait() (implemented here) is done by successive sleep with a delay of
min(delay * 2, remaining, .05)
where delay is originally set at0.0005 # 500 us -> initial delay of 1 ms
. According to this implementation (that is OS independant) we can argue that the accuracy is at least the time.sleep accuracy.But, the time.sleep() accuracy is based on the OS used (here is the implementation : floatsleep()), on Windows, it uses the
WaitForSingleObject()
with a internal windows timer, on Linux it uses theselect()
method.At least, because apart the sleep delay, the charge of the OS could interfer with the reactivity of the python process, the scheduling algorithm can also have an influence on the accuracy.