More , Please visit mine Personal blog .
Speaking of scheduled tasks , You may think of... First linux Medium crontab
, perhaps windows Task plan in . These tools are very convenient to use , But you may not believe it , Recently, I used... In the process of generating letters crontab
When the command completes some automatic operations , I have a problem .
I don't know if it is crontab
Command not allowed qsub Submit operation of , The administrator has set crontab
The user who initiated the task does not have node access ... All in all , It's always convenient crontab
The order dug a hole for me . therefore , I had to write a regular task myself .
Of course , The core function is based on today's protagonists APScheduler
Timing task framework .
Installation requires only one line of command .
pip3 install apscheduler
If the Python I am not familiar with the environment construction and module installation , Take a look at another blog I wrote Python Environment construction and module installation .
First, the two most commonly used schedulers are introduced :
Is that what people say ? I can read all the words , I don't understand the meaning at all ...
In a nutshell , You can put BlockingScheduler
Think of it as a single thread , If only scheduled tasks are run in the program , Then you should choose the blocking scheduler .
But the BackgroundScheduler
Think of it as multithreading , In addition to running scheduled tasks in the program , We also want to do some other calculations at the same time , Then you should choose the background scheduler .
Here I choose to use BlockingScheduler
Blocking scheduler , The main program is only responsible for scheduling scheduled tasks , Do not perform other calculations .
As shown below :
from apscheduler.schedulers.blocking import BlockingScheduler # Introduce modules
def task():
''' Timing task '''
os.system('python3 spider.py')
if __name__ == '__main__':
scheduler = BlockingScheduler()
# Add tasks
scheduler.add_job(task, 'cron', hour=11, minute=30)
scheduler.start()
Run the above code , Every day 11:30 When the python3 spider.py
command .
among , A new label appears cron
, This thing is called a trigger , You can set the conditions for triggering scheduled tasks , Here is a brief introduction to this little thing .
APScheduler There are three built-in triggers :
date , Trigger a scheduled task on a specific date , Trigger only once .
# stay 2020-1-3 In the early morning of the day task function
scheduler.add_job(task, 'date', run_date=date(2020, 1, 3))
# stay 1990-12-22 14:30:22 When the task function
scheduler.add_job(task, 'date', run_date='1990-12-22 14:30:22')
# Unspecified time , It will be executed immediately
scheduler.add_job(task, 'date')
As shown above ,run_date Parameters can be date type
or str type
, You can not even explicitly specify .
interval , Trigger a scheduled task after a certain time interval , The interval triggers an infinite number of times .
# every other 1 Zhou 3 God 8 when 20 branch 5 Once per second task function
scheduler.add_job(task, 'interval', weeks=1,days=3,hours=8,minutes=20,seconds=5)
As shown above ,weeks、days、hours、minutes、seconds The parameters of are int type
.
cycle , Trigger a scheduled task in a certain period , The loop triggers an infinite number of times .
# Every day 8 when 20 Execute once task function
scheduler.add_job(task, 'cron', hour=8,minute=20)
# Every day from Monday to Friday 8:20 Do it once task function , until 2100-05-20 Termination of procedure
scheduler.add_job(task, 'cron', day_of_week='mon-fri',hour=8,minute=20,end_date='2100-05-20')
The trigger's rules and crontab
similar . The description of each parameter is as follows :
More programming teaching, please pay attention to the official account : Pangao will accompany you to learn programming