简体   繁体   English

如何安排python函数使用apscheduler在特定分钟(例如00、10、20)每小时运行一次

[英]How to schedule a python function to run every hour at specific minute (ex 00, 10, 20) with apscheduler

I try to schedule a function within my script to run at a specific minute every hour, ie if my script starts running at 11.46 my function to run at 11.50, 12.00, 12.10 etc and to do the same thing if it fails and restarts. 我尝试将脚本中的函数安排为每小时在特定时间运行,即,如果脚本以11.46开始运行,则函数以11.50、12.00、12.10等运行,如果失败并重新启动,则执行相同的操作。 I have seen that it is possible to do this with cron from command line but is there a way to do it while the script is running with a scheduler? 我已经看到可以从命令行使用cron做到这一点,但是当脚本使用调度程序运行时,有没有办法做到这一点?

I have already created a scheduler which runs every 10 minutes after my script starts, but I want my function to run on specific minutes not just specific intervals. 我已经创建了一个调度程序,该脚本在脚本启动后每10分钟运行一次,但是我希望函数在特定的分钟上运行,而不仅仅是特定的间隔。

from apscheduler.schedulers.background import BackgroundScheduler
import atexit

def demo_job:
    print("test")

try:
    sched = BackgroundScheduler(daemon=True)
    sched.add_job(demo_job, trigger='interval', minutes=10)
    sched.start()
    atexit.register(lambda: sched.shutdown(wait=False))
except:
    print("Unexpected error:")

If my script starts running at 11.47 the first scheduled call of my function is at 11.57, the next at 12.07, 12.17 etc... I want the first run to be at 12.00, the next at 12.10, 12.20 etc 如果我的脚本在11.47开始运行,则我函数的第一个计划调用在11.57,下一次在12.07、12.17等...我希望第一次在12.00、12.10、12.20等运行。

You can try using the cron trigger . 您可以尝试使用cron触发器

sched.add_job(
    demo_job, 
    trigger='cron',
    minute='*/10',
    hour='*'
)

The expression */10 will fire the job at every tenth minute, starting from the minimum value. 从最小值开始,表达式*/10将每10分钟触发一次作业。 Crontab ref Crontab参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python - 计划:每小时运行 function + “整点” - Python - Schedule: Run function every hour + "on the hour" 如何安排一个函数在 Flask 上每小时运行一次? - How to schedule a function to run every hour on Flask? 如何使用调度库每分钟运行一次函数 - How to user Schedule Library to run function every minute 使用 APScheduler 计划在 Flask 上每小时运行的函数中的 SQLAlchemy 查询 - SQLAlchemy queries in a function scheduled to run every hour on Flask using APScheduler 安排一个 python 脚本每小时运行一次 - schedule a python script to run every hour 如何独立于程序运行开始时间使用apscheduler每隔一小时准确执行python函数 - How to execute a python function for every one hour exactly using apscheduler independent of program running start time 每分钟运行一个函数python? - Run a function every minute python? python apscheduler 不是每 1 分钟由 cron 执行一次 - python apscheduler not execute by cron for every 1 minute 使用 asyncio 在每分钟的开始(00 秒)运行一个函数 - Using asyncio to run a function at the start (00 seconds) of every minute 一分钟运行一次Python函数 - Run Python Function Every Second for A Minute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM