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

推荐订阅源

Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
有赞技术团队
有赞技术团队
罗磊的独立博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
U
Unit 42
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Recent Announcements
Recent Announcements
Y
Y Combinator Blog
Vercel News
Vercel News
Martin Fowler
Martin Fowler
V
V2EX
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
LangChain Blog
云风的 BLOG
云风的 BLOG
H
Hackread – Cybersecurity News, Data Breaches, AI and More
aimingoo的专栏
aimingoo的专栏
G
Google Developers Blog
The GitHub Blog
The GitHub Blog
N
Netflix TechBlog - Medium
Google DeepMind News
Google DeepMind News
雷峰网
雷峰网
阮一峰的网络日志
阮一峰的网络日志
F
Fortinet All Blogs

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
Scheduling Your Tasks with Apscheduler
2021-10-20 · via jdhao's digital space

In Python, to run a task periodically, we can use the package apscheduler.

Two schedulers are provided in this package, BackgroundScheduler and BlockingScheduler. BackgroundScheduler will run in the background in a non-blocking fashion. On the other hand, BlockingScheduler will block until the job assigned is finished.

Here is some demo code to showcase their usage:

from datetime import datetime

from apscheduler.schedulers.background import BackgroundScheduler, BlockingScheduler

scheduler = BlockingScheduler()
scheduler.add_job(func=my_job, trigger='interval', seconds=2, id='my custom task')
scheduler.start()


def my_job():
    print(f"custom job called at {datetime.now()}")

In the above code, my_job is the task we want to run. We use the interval trigger. Trigger defines when we will run the given job. By using interval trigger, the job provided will be run periodically at the specified interval. For example, in the above code, the job will be run every two seconds, and we will observe output like the following:

custom job called at 2021-01-16 17:25:45.701729
custom job called at 2021-01-16 17:25:47.701612
custom job called at 2021-01-16 17:25:49.701627
custom job called at 2021-01-16 17:25:51.701584

We can use print_jobs() to print job info for this scheduler.

There is another cron trigger. Cron trigger will run the job if the given constraints is met. Here is an example of using the cron trigger:

from datetime import datetime

from apscheduler.schedulers.background import BackgroundScheduler, BlockingScheduler

scheduler = BlockingScheduler()
scheduler.add_job(func=my_job, trigger='cron', second=30, id='my custom task')
scheduler.start()


def my_job():
    print(f"custom job called at {datetime.now()}")

In the above code, the job will run on every year, every month, every day, every hour, every minute, on the 30th second. An example output is like the following:

custom job called at 2021-01-16 17:39:30.001336
custom job called at 2021-01-16 17:40:30.001498
custom job called at 2021-01-16 17:41:30.001821
custom job called at 2021-01-16 17:42:30.001922

Another example:

scheduler.add_job(func=my_job, trigger='cron', minute=0, second=30, id='my custom task')

In the above code, the job will run on every year, every month, every day, every hour, on minute 0 and second 30. For example, it will run on 17:00:30, 18:00:30 etc. Output will be like the following:

custom job called at 2021-01-17 21:00:30.001602
custom job called at 2021-01-17 22:00:30.001254
custom job called at 2021-01-17 23:00:30.001562
custom job called at 2021-01-18 00:00:30.001559
custom job called at 2021-01-18 01:00:30.001399

Ref#