Pythonのschedモジュールで指定日時に関数を実行する(指定日時まで遅延させる)

この投稿は最終更新日から1年以上経過しています

import time
import sched

run_time = [
    '2021-03-17 13:54:00',
    '2021-03-17 13:54:10',
    '2021-03-17 13:55:45',
]

def func():
    print(time.time())

s = sched.scheduler(time.time)
for rt in run_time:
    t = time.strptime(rt, '%Y-%m-%d %H:%M:%S')
    t = time.mktime(t)
    s.enterabs(t, 1, func)
s.run()

実行結果

1615956840.0012357
1615956850.0087488
1615956945.0038419

メモ

環境

参考