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
メモ
- schedulerクラスの第1引数はtime.monotonicがデフォルトなのでtime.timeを設定
- 指定日時もUNIX時間に変換
環境
- Python 3.7.9
- Windows 10 Home