Unable to solve TypeError: unorderable types: time.struct_time() > float()

Unable to solve TypeError: unorderable types: time.struct_time() > float()

我想要一个在特定日期和时间 运行 的函数。我正在使用 scheduler.enterabs()。它给我以下错误:

START: 1561263892.1316419
Traceback (most recent call last):
  File "timer.py", line 78, in <module>
    scheduler.run()
  File "/usr/lib/python3.5/sched.py", line 137, in run
    if time > now:
TypeError: unorderable types: time.struct_time() > float()
import sched,time
scheduler = sched.scheduler(time.time, time.sleep)
def print_event():
    print('inside print_event')
    print ('EVENT:', time.time())

print ('START:', time.time())
scheduler.enterabs(time.strptime("2019-06-23 09:55:00", "%Y-%m-%d %I:%M:%S"),1,print_event)

scheduler.run()

我想在特定的日期和时间 运行 print_event()。请帮我解决这个问题。提前致谢:)

对于,scheduler.enterabs(time, priority, action, argument=(), kwargs={})

时间参数应该是与传递给构造函数的 timefunc 函数的 return 值兼容的数字类型。

因此,time.strptime 的 return 值是 struct_time,因为 return 由 gmtime() 或 localtime() 编辑,这与浮点值 return编辑者 time.time.

检查这个答案以获得更好的解释:

What does the Unorderable Type error mean in Python?

sched.schedulertimefunctime.time,它仅作为时间使用简单的浮点数。

这意味着给 scheduler.enterabs 的时间也必须是浮点数。 strptime returns struct_time 元组。

解决方案:使用time.mktime将元组转换为浮点时间。