【问题标题】:Print sched queue with human readable time打印具有人类可读时间的调度队列
【发布时间】:2021-11-13 17:26:54
【问题描述】:

我想用人类可读的时间从 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)

【问题讨论】:

  • 你得到了什么结果?代码似乎对我有用。

标签: python python-3.x scheduled-tasks sched


【解决方案1】:

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

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

【讨论】:

  • 我真的很抱歉,是的,这段代码正在按预期工作,我使用的是 enterabs 而不是 enter 所以事件时间是等待的秒数而不是时间戳
猜你喜欢
  • 2018-02-24
  • 2013-02-22
  • 1970-01-01
  • 2014-12-20
  • 2015-04-25
  • 2013-01-01
  • 1970-01-01
  • 2017-10-19
  • 1970-01-01
相关资源
最近更新 更多