【发布时间】:2017-10-24 06:00:12
【问题描述】:
我有一个调度函数和一个调度程序,其中包含按时间排序的未来事件队列。我正在使用 UNIX 时间戳和常规的 time.time()。调度器的一个片段大致相当于这个:
# select the nearest event (eventfunc at eventime)
sleeptime = eventtime - time.time()
# if the sleep gets interrupted,
# the whole block will be restarted
interruptible_sleep(sleeptime)
eventfunc()
eventtime 可以基于延迟计算:
eventtime = time.time() + delay_seconds
或基于确切的日期和时间,例如:
eventtime = datetime(year,month,day,hour,min).timestamp()
现在我们有了 Python 中的单调时间。我正在考虑修改调度程序以使用单调时间。调度程序应该使用他们所说的单调时间。
延迟没问题:
sleeptime = eventtime - time.monotonic()
地点:
eventtime = time.monotonic() + delay_seconds
但我认为最好的方法是让代码保持原样。对吗?
如果是,我需要两个事件队列,一个基于单调时间,一个基于常规时间。我不太喜欢这个主意。
【问题讨论】:
-
你不妨看看
schedstandard module。它被设计为独立于时间操作后端,默认使用time.monotonic。