惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MyScale Blog
MyScale Blog
J
Java Code Geeks
Vercel News
Vercel News
A
About on SuperTechFans
G
Google Developers Blog
C
Check Point Blog
腾讯CDC
N
Netflix TechBlog - Medium
博客园 - 司徒正美
S
SegmentFault 最新的问题
D
DataBreaches.Net
博客园_首页
美团技术团队
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
量子位
雷峰网
雷峰网
IT之家
IT之家
小众软件
小众软件
Blog — PlanetScale
Blog — PlanetScale
博客园 - 三生石上(FineUI控件)
H
Help Net Security
宝玉的分享
宝玉的分享
博客园 - 叶小钗

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Run the Job Immediately after Starting Scheduler in Pytho...
2024-11-02 · via jdhao's digital space

When using APScheduler package in Python, I want to run the scheduled job right after I start the scheduler. How can I do it properly?

next_run_time param in add_job method#

In the scheduler, its add_job1 method has a parameter next_run_time. If we specify now time as the value, the job will run immediately.

from datetime import datetime
import time

from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.interval import IntervalTrigger


def my_job():
    print("job running at ", datetime.now())


trigger = IntervalTrigger(seconds=5)
scheduler = BackgroundScheduler()
scheduler.add_job(
    func=my_job,
    trigger=trigger,
    next_run_time=datetime.now(),
)

scheduler.start()

while True:
    time.sleep(2)

If we run the program, the scheduled job should run immediately.

start_date in IntervalTrigger#

In the interval trigger itself, it has a parameter start_date to specify when the job run should be triggered. The time can be a past datetime based doc:

If the start date is in the past, the trigger will not fire many times retroactively but instead calculates the next run time from the current time, based on the past start time.

Can we manipulate the start_date parameter to make the job run immediately? Sort of. We can use a past time and let the next run be right after we run the program.

import datetime
import time


from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.interval import IntervalTrigger


def my_job():
    print("hello, job run at %s", datetime.datetime.now())


amount = 5
eps = 0.01
my_trigger = IntervalTrigger(
    seconds=amount,
    start_date=datetime.datetime.now() - datetime.timedelta(seconds=amount - eps),
)

scheduler = BackgroundScheduler()
scheduler.start()

scheduler.add_job(
    func=my_job,
    trigger=my_trigger,
)

print("current time:", datetime.datetime.now())

while True:
    time.sleep(2)

In the above code, we try to set start_date to a past time. The eps variable is used to fine-tuning the time. If I set it to very small value like 0.01, it works. However, if I set it to 0.0, it does not work, the job will run after amount time. So this is not a reliable way to run the job right after we launch the program.

ref#