apscheduler在指定时间每X天启动一次



我需要让我的调度程序每X天在特定的时间启动一次(例如每7天在11:30)
我的代码:

def make_interval(record_date: str, record_time: str, record_title: str):
hours, minutes = _get_hours_minutes(record_time)
trigger = AndTrigger([IntervalTrigger(days=int(record_date)),
CronTrigger(hour=hours, minute=minutes)])
scheduler.add_job(_send_notification, trigger=trigger,
kwargs={...},
id=record_title,
timezone=user_timezone)

,但我得到错误:[Errno 22] Invalid argument在第三行。
怎么了?不明白为什么它不工作

_get_hours_minutes(仅从"HH:MM"分别返回小时和分钟的值)

def _get_hours_minutes(user_time: str) -> (str, str):
return user_time[:2], user_time[3:5]

回溯:

Traceback (most recent call last):
File "C:Userspizhlo21DesktopFolderpythontg_bot_reminderschedulermain.py", line 92, in make_interval
scheduler.add_job(_send_notification, trigger=trigger,
File "C:Userspizhlo21DesktopFolderpythontg_bot_remindervenvLibsite-packagesapschedulerschedulersbase.py", line 447, in add_job
self._real_add_job(job, jobstore, replace_existing)
File "C:Userspizhlo21DesktopFolderpythontg_bot_remindervenvLibsite-packagesapschedulerschedulersbase.py", line 863, in _real_add_job
replacements['next_run_time'] = job.trigger.get_next_fire_time(None, now)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:Userspizhlo21DesktopFolderpythontg_bot_remindervenvLibsite-packagesapschedulertriggerscombining.py", line 55, in get_next_fire_time
fire_times = [trigger.get_next_fire_time(previous_fire_time, now)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:Userspizhlo21DesktopFolderpythontg_bot_remindervenvLibsite-packagesapschedulertriggerscombining.py", line 55, in <listcomp>
fire_times = [trigger.get_next_fire_time(previous_fire_time, now)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:Userspizhlo21DesktopFolderpythontg_bot_remindervenvLibsite-packagesapschedulertriggersinterval.py", line 68, in get_next_fire_time
return normalize(next_fire_time)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:Userspizhlo21DesktopFolderpythontg_bot_remindervenvLibsite-packagesapschedulerutil.py", line 431, in normalize
return datetime.fromtimestamp(dt.timestamp(), dt.tzinfo)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [Errno 22] Invalid argument

我的操作系统平台:Windows 10

我会仔细看看"record_time"传递给"_get_hours_minutes"的格式。格式必须为"HH:MM"(假设Errno 22错误是针对那一行的。还有其他方法可以使用datetime库,这样您就不必对字符串进行切片。

相关内容

  • 没有找到相关文章