简体   繁体   English

threading.Timer时间精度

[英]threading.Timer time precision

What does the time accuracy of Python's threading.Timer depend on? Python的threading.Timer的时间准确度取决于什么?

Does it depend on the OS used? 它取决于使用的操作系统吗?
Does it depend on the Python implementation used? 它取决于使用的Python实现吗?
Does it depend on the interval? 它取决于间隔吗?
How can it be benchmarked? 如何进行基准测试?
Are there already benchmarks out there? 那里有基准吗?

In CPython, the threading.Timer accuracy ( waiting part ) is based on a Condition.wait() call. 在CPython中,threading.Timer准确性( 等待部分 )基于Condition.wait()调用。

The Condition.wait() ( implemented here ) is done by successive sleep with a delay of min(delay * 2, remaining, .05) where delay is originally set at 0.0005 # 500 us -> initial delay of 1 ms . Condition.wait()( 此处实现 )通过连续睡眠完成,延迟为min(delay * 2, remaining, .05) ,其中延迟最初设置为0.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. 根据这个实现(即OS独立),我们可以说精确度至少是time.sleep准确性。

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 the select() method. 但是,time.sleep()的准确性是基于所使用的操作系统( 这里是实现:floatsleep() ),在Windows上,它使用带有内部Windows计时器的WaitForSingleObject() ,在Linux上它使用select()方法。

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. 至少,因为除睡眠延迟之外,OS的电荷可能干扰python过程的反应性,调度算法也可能对准确性产生影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM