current position:Home>The apscheduler module in Python implements scheduled tasks
The apscheduler module in Python implements scheduled tasks
2022-01-31 17:39:29 【Notes of the lost schoolboy】
This is my participation 11 The fourth of the yuegengwen challenge 17 God , Check out the activity details :2021 One last more challenge
Hardware and software environment
- windows 10 64bits
- anaconda with python 3.7
- apscheduler 3.6.3
Preface
Speaking of scheduled tasks , The first reaction should be windows
Own planned tasks or linux
Self contained crontab
, About ubuntu
How to use... Under the operating system crontab
You can refer to the following video . apscheduler
It's a utility model python
Timed task tool developed by language , It provides a very rich and easy-to-use timing task interface
install
Very simple installation , Use pip
pip install apscheduler
Copy code
apscheduler Four components of
- triggers trigger By date 、 Time interval or
contab
Expressions trigger in three ways - job stores Job memory Specify the location where the job is stored , It is saved in memory by default , It can also be saved in various databases
- executors actuator Submit the specified job to thread pool or process pool to run
- schedulers Job scheduler Commonly used
BackgroundScheduler
( Background operation ) andBlockingScheduler
( Blocking type )
Code practice
Here are a few examples to see how to use apscheduler
import time
from apscheduler.schedulers.background import BlockingScheduler
from apscheduler.triggers.interval import IntervalTrigger
def my_job():
print('my_job, {}'.format(time.ctime()))
if __name__ == "__main__":
scheduler = BlockingScheduler()
# The interval is set to 1 second , You can also use minutes、hours、days、weeks etc.
intervalTrigger=IntervalTrigger(seconds=1)
# Set a for homework id, Facilitate the subsequent operation of the operation , Pause 、 Cancel waiting
scheduler.add_job(my_job, intervalTrigger, id='my_job_id')
scheduler.start()
print('=== end. ===')
Copy code
Execute code , The output looks like this
Because we used BlockingScheduler
, It's blocking , So I only saw my_job
Output in method , And the statement print('=== end. ===')
Not implemented .BackgroundScheduler
It can run in the background , Does not block the execution of the main thread , Look at the following code
import time
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.interval import IntervalTrigger
def my_job():
print('my_job, {}'.format(time.ctime()))
if __name__ == "__main__":
scheduler = BackgroundScheduler()
intervalTrigger=IntervalTrigger(seconds=1)
scheduler.add_job(my_job, intervalTrigger, id='my_job_id')
scheduler.start()
print('=== end. ===')
while True:
time.sleep(1)
Copy code
The result of code execution is this
If you put triggers
Set to DateTrigger
, It becomes that the job is executed at a certain point in time , Examples are as follows
import time
import datetime
from apscheduler.schedulers.background import BlockingScheduler
from apscheduler.triggers.date import DateTrigger
def my_job():
print('my_job, {}'.format(time.ctime()))
if __name__ == "__main__":
scheduler = BlockingScheduler()
intervalTrigger=DateTrigger(run_date='2020-07-17 16:18:55')
scheduler.add_job(my_job, intervalTrigger, id='my_job_id')
scheduler.start()
Copy code
Wait until the set time comes , The homework will be done by itself once
If you want to follow the specified cycle , You need to use CronTrigger
了 , The working principle is the same as UNIX
in crontab
Timed tasks are very similar , It can specify very detailed and complex rules . Similarly, look at the sample code
import time
from apscheduler.schedulers.background import BlockingScheduler
from apscheduler.triggers.cron import CronTrigger
def my_job():
print('my_job, {}'.format(time.ctime()))
if __name__ == "__main__":
scheduler = BlockingScheduler()
# Execute the job in the first second
intervalTrigger=CronTrigger(second=1)
# Daily 19:30:01 Perform operation
# intervalTrigger=CronTrigger(hour=19, minute=30, second=1)
# From year to year 10 month 1 Japan 19 Click to execute the job
# intervalTrigger=CronTrigger(month=10, day=1, hour=19)
scheduler.add_job(my_job, intervalTrigger, id='my_job_id')
scheduler.start()
Copy code
The effect of code execution is like this
The code above does not use executors
, Because there is only one homework , But from the debugging, we can find that , By default ,apscheduler
It's also used ThreadPoolExecutor
, And the size of the thread pool is 10
So let's see executors
Use
import time
from apscheduler.schedulers.background import BlockingScheduler
from apscheduler.triggers.interval import IntervalTrigger
from apscheduler.executors.pool import ThreadPoolExecutor
def my_job():
print('my_job, {}'.format(time.ctime()))
if __name__ == "__main__":
executors = {
'default': ThreadPoolExecutor(20)
}
scheduler = BlockingScheduler(executors=executors)
intervalTrigger=IntervalTrigger(seconds=1)
scheduler.add_job(my_job, intervalTrigger, id='my_job_id')
scheduler.start()
Copy code
You can see , Let's change the size of the thread pool to 20, Initializing scheduler
Will be executors
Pass in , The following operation is exactly the same as before
About ThreadPoolExecutor
and ProcessPoolExecutor
The choice of , Here's a principle , If it is cpu
Intensive work , Use ProcessPoolExecutor
, Other uses ThreadPoolExecutor
, Of course ThreadPoolExecutor
and ProcessPoolExecutor
It can also be mixed
Finally, let's look at the job memory , We changed it to store it in sqlite
in
import time
from apscheduler.schedulers.background import BlockingScheduler
from apscheduler.triggers.interval import IntervalTrigger
from apscheduler.executors.pool import ThreadPoolExecutor
from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
def my_job():
print('my_job, {}'.format(time.ctime()))
jobstores = {
'default': SQLAlchemyJobStore(url='sqlite:///jobs.sqlite')
}
if __name__ == "__main__":
executors = {
'default': ThreadPoolExecutor(20)
}
scheduler = BlockingScheduler(jobstores=jobstores, executors=executors)
intervalTrigger=IntervalTrigger(seconds=1)
scheduler.add_job(my_job, intervalTrigger, id='my_job_id')
scheduler.start()
Copy code
After code execution , Will be generated in the source directory sqlite
Database files jobs.sqlite
, We use graphical tools to open and view , You can see the of jobs stored in the database id
And the next execution time of the job
Reference material
copyright notice
author[Notes of the lost schoolboy],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201311739258671.html
The sidebar is recommended
- Python - convert Matplotlib image to numpy Array or PIL Image
- Python and Java crawl personal blog information and export it to excel
- Using class decorators in Python
- Untested Python code is not far from crashing
- Python efficient derivation (8)
- Python requests Library
- leetcode 2047. Number of Valid Words in a Sentence(python)
- leetcode 2027. Minimum Moves to Convert String(python)
- How IOS developers learn Python Programming 5 - data types 2
- leetcode 1971. Find if Path Exists in Graph(python)
guess what you like
-
leetcode 1984. Minimum Difference Between Highest and Lowest of K Scores(python)
-
Python interface automation test framework (basic) -- basic syntax
-
Detailed explanation of Python derivation
-
Python reptile lesson 2-9 Chinese monster database. It is found that there is a classification of color (he) desire (Xie) monsters during operation
-
A brief note on the method of creating Python virtual environment in Intranet Environment
-
[worth collecting] for Python beginners, sort out the common errors of beginners + Python Mini applet! (code attached)
-
[Python souvenir book] two people in one room have three meals and four seasons: 'how many years is it only XX years away from a hundred years of good marriage' ~?? Just come in and have a look.
-
The unknown side of Python functions
-
Python based interface automation test project, complete actual project, with source code sharing
-
A python artifact handles automatic chart color matching
Random recommended
- Python crawls the map of Gaode and the weather conditions of each city
- leetcode 1275. Find Winner on a Tic Tac Toe Game(python)
- leetcode 2016. Maximum Difference Between Increasing Elements(python)
- Run through Python date and time processing (Part 2)
- Application of urllib package in Python
- Django API Version (II)
- Python utility module playsound
- Database addition, deletion, modification and query of Python Sqlalchemy basic operation
- Tiobe November programming language ranking: Python surpasses C language to become the first! PHP is about to fall out of the top ten?
- Learn how to use opencv and python to realize face recognition!
- Using OpenCV and python to identify credit card numbers
- Principle of Python Apriori algorithm (11)
- Python AI steals your voice in 5 seconds
- A glance at Python's file processing (Part 1)
- Python cloud cat
- Python crawler actual combat, pyecharts module, python data analysis tells you which goods are popular on free fish~
- Using pandas to implement SQL group_ concat
- How IOS developers learn Python Programming 8 - set type 3
- windows10+apache2. 4 + Django deployment
- Django parser
- leetcode 1560. Most Visited Sector in a Circular Track(python)
- leetcode 1995. Count Special Quadruplets(python)
- How to program based on interfaces using Python
- leetcode 1286. Iterator for Combination(python)
- leetcode 1418. Display Table of Food Orders in a Restaurant (python)
- Python Matplotlib drawing histogram
- Python development foundation summary (VII) database + FTP + character coding + source code security
- Python modular package management and import mechanism
- Django serialization (II)
- Python dataloader error "dataloader worker (PID XXX) is killed by signal" solution
- apache2. 4 + Django + windows 10 Automated Deployment
- leetcode 1222. Queens That Can Attack the King(python)
- leetcode 1387. Sort Integers by The Power Value (python)
- Tiger sniffing 24-hour praise device, a case with a crawler skill, python crawler lesson 7-9
- Python object oriented programming 01: introduction classes and objects
- Baidu Post: high definition Python
- Python Matplotlib drawing contour map
- Python crawler actual combat, requests module, python realizes IMDB movie top data visualization
- Python classic: explain programming and development from simple to deep and step by step
- Python implements URL availability monitoring and instant push