程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Use of Python scheduled task apscheduler

編輯:Python

apscheduler Use

We can't avoid using some scheduled tasks in our projects , For example, recent projects , After users click to register for the exam, they need to push a small program message to remind customers on wechat when the exam date is approaching , Turn over fastapi In the implementation of , Although there are many methods and packages , But either it's too heavy ( For example, you need to open another service , Also rely on redis, It's not easy to use ), Although it can also be used time Modular time.sleep() On board fastapi The background task of is realized in disguise , But the relatively simple function is OK , Complex code is troublesome , So we'd better assign someone to find a bag responsible for this amount . Look around and find out APScheduler It's quite suitable , The code is simple , The implementation effect is also very good , Here's a record !
install

pip install apscheduler

Major component

Conceptual stuff , I'm familiar with you , Code is easier to understand than these definitions .

trigger (trigger) Contains scheduling logic , Each job has its own trigger , Used to decide which job will run next . Except for their own initial configuration , Triggers are completely stateless . Speaking human is the way you specify to trigger the current task .
type explain
DateTrigger Due execution ( To xxxx year x month x Japan x when x branch x Seconds to perform ) Corresponding DateTrigger
IntervalTrigger Interval execution ( Every time 5 Once per second )
CronTrigger One crontab Conditions of type ( This is more complicated , For example, Monday to Thursday 4-5 Dot per hour 5 Once per second )
https://www.wenjuan.ltd/s/UZB...
https://www.wenjuan.ltd/s/UZB...
https://www.wenjuan.ltd/s/UZB...
https://www.wenjuan.ltd/s/UZB...
https://www.wenjuan.ltd/s/UZB...
https://www.wenjuan.ltd/s/UZB...
https://www.wenjuan.com/s/UZB...
https://www.wenjuan.com/s/UZB...
https://www.wenjuan.com/s/UZB...
https://www.wenjuan.com/s/UZB...
https://www.wenjuan.com/s/UZB...
https://www.wenjuan.com/s/UZB...
https://www.wenjuan.com/s/UZB...
https://www.wenjuan.com/s/UZB...
https://www.wenjuan.com/s/UZB...
https://www.wenjuan.com/s/UZB...
https://www.wenjuan.com/s/UZB...
https://www.wenjuan.com/s/UZB...
https://www.wenjuan.com/s/UZB...
https://www.wenjuan.com/s/UZB...
https://www.wenjuan.com/s/UZB...
https://www.wenjuan.com/s/UZB...
https://www.wenjuan.com/s/UZB...
https://www.wenjuan.com/s/UZB...
https://www.wenjuan.com/s/UZB...
Job storage (job store) Store scheduled jobs , The default job store is simply to save jobs in memory , Other job stores store jobs in databases . The data of a job is serialized when it is stored in a persistent job store , And is deserialized on load . The scheduler cannot share the same job store .

Jobstore stay scheduler In the initialization , In addition, it can also be through scheduler Of add_jobstore Dynamic addition Jobstore. Every jobstore
Will be bound to a alias,scheduler stay Add Job when , According to the designation jobstore stay scheduler Find the corresponding jobstore, and
take job Add to jobstore in .

Jobstore Mainly through pickle Library loads and dumps【 The core of the implementation is through python Of __getstate__ and __setstate__ rewrite
Realization 】, Each change will Job Save to dynamic storage , When in use, it will be loaded dynamically , As storage can be redis, It's fine too
It's the database 【 adopt sqlarchemy This library integrates multiple databases 】, It can also be mongodb etc.
at present APScheduler Supported by Jobstore:

MemoryJobStore
MongoDBJobStore
RedisJobStore
RethinkDBJobStore
SQLAlchemyJobStore
ZooKeeperJobStore

actuator (executor) Process the run of the job , They usually submit the specified callable objects to a thread or pool in the job . When the work is done , The actuator will inform the scheduler .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved