How to use Python timing Library Apscheduler
Xiaobian to share with you how to use Python timing library Apscheduler, I hope you have something to gain after reading this article, let's discuss it together!
In Python, you need to perform timed tasks, you can use Apscheduler.
Apscheduler is a Python timed task framework based on Quartz, which is consistent with Quartz in function and almost consistent with Quartz in use.
Four core components:
① Trigger, ② job store, ③ executor, ④ scheduler
Installation dependencies:
pip install apscheduler
Interval scheduling:
from apscheduler.schedulers.blocking import BlockingSchedulerfrom datetime import datetime sched = BlockingScheduler()def test_job(): print(f'{datetime.now():%H:%M:%S} Test job') if __name__ == '__main__': sched.add_job(test_job, 'interval', id='test', seconds=5) sched.start()
Decorators can also be used:
from apscheduler.schedulers.blocking import BlockingSchedulerfrom datetime import datetime sched = BlockingScheduler() def test_job(): print(f'{datetime.now():%H:%M:%S} Test job') @sched.scheduled_job('interval', seconds=5)def test_decorator_job(): print(f'{datetime.now():%H:%M:%S} Test decorator job') if __name__ == '__main__': sched.add_job(test_job, 'interval', id='test', seconds=5) sched.start()
Run Results:
BlockingScheduler() is a scheduler within schedulers
sched.add_job() is an add job
sched.start() is the start task
Timed scheduling:
Timing scheduling is performed using cron expressions, which are also executed with parameters:
from apscheduler.schedulers.blocking import BlockingSchedulerfrom datetime import datetime scheduler = BlockingScheduler() def test_args(x): print (f'{datetime.now():%H:%M:%S} Test cron job', x) if __name__ == '__main__': scheduler.add_job(test_args, 'cron', args=('cron test',), second='*/5') scheduler.start()
Time parameters are set as follows:
year (int| str) -year, 4 digit month (int| str) -month (range 1-12) day (int| str) -day (range 1-31) week (int| str) -week (range 1-53) day_of_week (int| str) -Day of the week (range 0-6 or mon,tue,wed,thu,fri,sat,sun) hour (int)| str) -hour (range 0-23) minute (int| str) -min (range 0-59) second (int| str) -seconds (range 0-59) start_date (datetime| str) -earliest start date (inclusive) end_date (datetime| str) -latest end time inclusive timezone (datetime.tzinfo| str) -Specify time zone
Next, let's talk about the scheduler:
BlockingScheduler: applies to the scheduler is the only process running in the process, calling the start function will block the current thread, can not immediately return. BackgroundScheduler: Used when the scheduler runs in the background of the application, and the main thread does not block after calling start. AsyncIOScheduler: For applications that use the asyncio module. GeventScheduler: For applications that use the gevent module. Twisted Scheduler: For building Twisted applications. QtScheduler: Applications for building Qt.
Among them, the first three schedulers are used more frequently.
Delete Task:
scheduler.remove_job('task_id')
Stop Mission:
scheduler.pause_job('task_id')
Recovery Quest:
scheduler.resume_job('task_id')
Run the task immediately next_run_time:
scheduler.add_job( test_job, 'interval', minutes=5, next_run_time=datetime.datetime.now() After reading this article, I believe you have a certain understanding of "how to use Python timer Apscheduler". If you want to know more about it, please pay attention to the industry information channel. Thank you for reading!