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

Introduction to apscheduler in Python

編輯:Python

Preface

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 .

install

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 .

function

First, the two most commonly used schedulers are introduced :

  • BlockingScheduler
    Blocking scheduler : For programs that only run the scheduler .
  • BackgroundScheduler
    Background scheduler : For non blocking situations , The scheduler will run independently in the background .

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

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

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 .

cron

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 :

Parameters explain yearint Type or str, Four digit year , Such as 2020 year monthint Type or str, The value range is 1-12 month weekint Type or str, The range of values is 1-53 Zhou day_of_weekint Type or str, Represents the day of the week , You can use 0-6 It can also be expressed by its English abbreviation (mon,tue,wed,thu,fri,sat,sun)dayint Type or str, The value range is 1-31 Japan hourint Type or str, The value range is 0-23 when minuteint Type or str, The value range is 0-59 branch secondint Type or str, The value range is 0-59 second start_datedatetime Type or str, Indicates the start time end_datedatetime Type or str, Indicates the end time

More programming teaching, please pay attention to the official account : Pangao will accompany you to learn programming



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