python高精度定时器中断_计时器中断线程python

博客探讨了在Python中创建精确定时任务的挑战。作者尝试使用`threading.Timer`和单独的线程来实现每秒执行一次的任务,但遇到了时间漂移问题。解决方案包括在循环中每4秒启动新线程,以及根据工作负载调整睡眠时间以保持定时精度。文章讨论了线程同步和避免时间漂移的方法。
摘要由CSDN通过智能技术生成

I've been trying to make a precise timer in python, or as precise a OS allows it to be. But It seams to be more complicated than I initially thought.

This is how I would like it to work:

from time import sleep

from threading import Timer

def do_this():

print ("hello, world")

t = Timer(4, do_this)

t.start()

sleep(20)

t.cancel()

Where during 20 seconds I would execute 'do_this' every fourth second. However 'do_this' executes once then the script terminates after 20 seconds.

Another way would be to create a thred with a while loop.

import time

import threading

import datetime

shutdown_event = threading.Event()

def dowork():

while not shutdown_event.is_set():

print(datetime.datetime.now())

time.sleep(1.0)

def main():

t = threading.Thread(target=dowork, args=(), name='worker')

t.start()

print("Instance started")

try:

while t.isAlive():

t.join(timeout=1.0)

except (KeyboardInterrupt, SystemExit):

shutdown_event.set()

pass

if __name__ == '__main__':

main()

This thread executes as expected but I get a timing drift. I in this case have to compensate for the time it takes to execute the code in the while loop by adjusting the sleep accordingly.

Is there a simple way in python to execute a timer every second (or any interval) without introducing a drift compared to the system time without having to compensate the sleep(n) parameter?

Thanks for helping,

/Anders

解决方案

If dowork() always runs in less time than your intervals, you can spawn a new thread every 4 seconds in a loop:

def dowork():

wlen = random.random()

sleep(wlen) # Emulate doing some work

print 'work done in %0.2f seconds' % wlen

def main():

while 1:

t = threading.Thread(target=dowork)

time.sleep(4)

If dowork() could potentially run for more than 4 seconds, then in your main loop you want to make sure the previous job is finished before spawning a new one.

However, time.sleep() can itself drift because no guarantees are made on how long the thread will actually be suspended. The correct way of doing it would be to figure out how long the job took and sleep for the remaining of the interval. I think this is how UI and game rendering engines work, where they have to display fixed number of frames per second at fixed times and rendering each frame could take different length of time to complete.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值