打印具有人类可读时间的调度队列

Print sched queue with human readable time

我想用人类可读的时间从 sched 调度程序打印队列。 但是无论我尝试什么,我都会得到奇怪的结果,尽管事件是在适当的时间和顺序执行的。

如何实现?

当前代码

scheduler = sched.scheduler(timefunc=time.time)

# Add some events into a scheduler

def executing_at_message(event):
    date = datetime.fromtimestamp(event.time)
    print(f"{date.hour}:{date.minute}: {event.action.__doc__}")

for event in scheduler.queue:
    executing_at_message(event)

除了添加实际工作之外,我没有做任何更改,这似乎工作正常。

import sched
from datetime import datetime
import time

scheduler = sched.scheduler(timefunc=time.time)

def main():
    """Do the main thing"""
    time.sleep(3)
    print('Done main')

def other():
    """Do the other thing"""
    time.sleep(2)
    print('Done other')

scheduler.enter(3, 2, main)
scheduler.enter(2, 1, other)

def executing_at_message(event):
    date = datetime.fromtimestamp(event.time)
    print(f"{date.hour}:{date.minute}:{date.second} {event.action.__doc__}")

for event in scheduler.queue:
    executing_at_message(event)

输出:

22:41:45 Do the other thing
22:41:46 Do the main thing