Apscheduler cron 在半小时开始
Apscheduler cron to start on the half hour
我希望有一个 运行 基于股市开盘和收盘时间 (8:30-3 CST) 的 cron 作业。我希望它每 15 分钟 运行,从 8:30.
开始
我目前拥有的是
sched.add_job(scheduled_task,'cron',
day_of_week='mon-fri', hour='8-15',
minute=enter code here'0-59/15',
timezone='America/Chicago')
这允许我每 15 分钟 运行 上午 8 点到下午 3 点,这不是我想要的。我还尝试了以下方法:
sched.add_job(scheduled_task,'cron',
day_of_week='mon-fri', hour='12-20',
minute='30/15',
timezone='America/Chicago')
这让我更接近了,但在第 30 分钟和第 45 分钟只有 运行 秒。
我会说 运行 你的 cron 任务每 15 分钟一次,当当前时间早于 8:30 (8 * 60 + 30)[=11 时跳过代码的执行=]
import datetime
from apscheduler.scheduler import Scheduler
cron = Scheduler(daemon=True)
cron.start()
@cron.interval_schedule(minutes=15)
def job_function():
now = datetime.datetime.now()
if (now.hour * 60 + now.minute) > 8 * 60 + 30:
return
# code to execute
解决方法是使用OrTrigger:
from apscheduler.triggers.cron import CronTrigger
from apscheduler.triggers.combining import OrTrigger
cron1 = CronTrigger(day_of_week='mon-fri', hour='8', minute='30,45', timezone='America/Chicago')
cron2 = CronTrigger(day_of_week='mon-fri', hour='9-15', minute='*/15', timezone='America/Chicago')
trigger = OrTrigger([cron1, cron2])
sched.add_job(scheduled_task, trigger)
我希望有一个 运行 基于股市开盘和收盘时间 (8:30-3 CST) 的 cron 作业。我希望它每 15 分钟 运行,从 8:30.
开始我目前拥有的是
sched.add_job(scheduled_task,'cron',
day_of_week='mon-fri', hour='8-15',
minute=enter code here'0-59/15',
timezone='America/Chicago')
这允许我每 15 分钟 运行 上午 8 点到下午 3 点,这不是我想要的。我还尝试了以下方法:
sched.add_job(scheduled_task,'cron',
day_of_week='mon-fri', hour='12-20',
minute='30/15',
timezone='America/Chicago')
这让我更接近了,但在第 30 分钟和第 45 分钟只有 运行 秒。
我会说 运行 你的 cron 任务每 15 分钟一次,当当前时间早于 8:30 (8 * 60 + 30)[=11 时跳过代码的执行=]
import datetime
from apscheduler.scheduler import Scheduler
cron = Scheduler(daemon=True)
cron.start()
@cron.interval_schedule(minutes=15)
def job_function():
now = datetime.datetime.now()
if (now.hour * 60 + now.minute) > 8 * 60 + 30:
return
# code to execute
解决方法是使用OrTrigger:
from apscheduler.triggers.cron import CronTrigger
from apscheduler.triggers.combining import OrTrigger
cron1 = CronTrigger(day_of_week='mon-fri', hour='8', minute='30,45', timezone='America/Chicago')
cron2 = CronTrigger(day_of_week='mon-fri', hour='9-15', minute='*/15', timezone='America/Chicago')
trigger = OrTrigger([cron1, cron2])
sched.add_job(scheduled_task, trigger)