from datetime import date
import datetime as dt
from apscheduler.schedulers.blocking import BlockingScheduler
sched = BlockingScheduler()
def my_job(text):
print(f"{dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')} text")
# scheduled_job()装饰器实现
@sched.scheduled_job('interval', id='my_job_id', hours=2)
def job_function():
print("Hello World")
# 未指定时间,则会立即执行
# sched.add_job(my_job, args=['text'])
# 在2009年11月6日执行 精度到天
# sched.add_job(my_job, 'date', run_date=date(2023, 11, 27), args=['text'])
# 指定时间运行,精度到秒
# sched.add_job(my_job, 'date', run_date=dt.datetime(2023, 11, 27, 10, 3, 0), args=['text'])
#
# 每隔5秒钟执行一次
# sched.add_job(my_job, 'interval', seconds=3,args=['text'])
# 每隔10秒钟执行一次,开始时间为2023-11-27 10:10:00,结束时间为2023-11-27 11:15:00,jitter为5秒钟 jitter为震荡时间,即每次执行时间的随机值在-jitter和jitter之间
sched.add_job(my_job, 'interval', seconds=10, start_date='2023-11-27 10:10:00', end_date='2023-11-27 11:15:00',
args=['text'], jitter=5)
# 启动
sched.start()