text: current position：Home>Test platform series (71) Python timed task scheme
  Test platform series (71) Python timed task scheme
  2022-01-31 02:42:09 【Milo】
  This is my participation 11 The fourth of the yuegengwen challenge 4 God , Check out the activity details ：2021 One last more challenge
  Hello everyone ~ I am a
  Milo！
  I'm from 0 To 1 Build an open source interface testing platform , Also writing a complete set of corresponding
  course, I hope you can support me more .
  Welcome to my official account.
  Test and development pit goods, Get the latest articles and tutorials !
  Timing task
  Timing task , seeing the name of a thing one thinks of its function : A scheduled task , It can be a paragraph bash command , It can also be a script file . Usually used for things we need to do at a specific time .
  for instance : Every night 8 Click to execute the whole business scenario
  Interface automated regression testing , Or every 20 Go to your house in ten minutes QQ Stealing vegetables from the farm ！
  Python Scheduled tasks for
  If it is a large and medium-sized company , There must be a corresponding
  platform Let everyone access . Generally, the name or description of such a platform is :
  Distributed task scheduling platform In this way . If not, there are jenkins Meet our needs ~
  Think of bloggers before " When I was a child "（ At work ）, The company is useless
  jenkins, There is no such big platform to access . How do I do scheduled tasks ？
  Look at a piece of code :
  import time from datetime import datetime def func(): # This is a scheduled task to be performed ui/ Interface automation method pass def main(): while True: # Get the current time now = datetime.now() if now.hour == 20 and now.minute == 0: # When time arrives at night 8 spot 0 branch , We started running automated tasks print(" The task begins ") func() time.sleep(60) if __name__ == "__main__": main() Copy code
  I don't know if anyone still uses this way
  Timing execution automated testing , Since all 2021 Years. , Let's go a little further . So the scheduled task is Python How to do it in ？ Then look at .
  Several common schemes
  schedule
  If you just
  above-mentioned Of
  Simple appeal , Then I suggest using
  schedule library . My personal feeling about this library is
  Primary school students Version of the upgrade , For my limited sleep The task has made some extensions :
  - Supports the execution of tasks by cycle
  - The execution time is more user-friendly
  - Also support week X perform
  - install scheduler
  pip install schedule Copy code
  - An example from the official website
  import schedule import time # Define an execution method def job(): print("I'm working...") # Every time 10 Once per minute job schedule.every(10).minutes.do(job) # Once an hour job schedule.every().hour.do(job) # Every day 10:30 perform job schedule.every().day.at("10:30").do(job) # Once a week schedule.every().monday.do(job) # Every Wednesday 13:15 Do it once schedule.every().wednesday.at("13:15").do(job) # In every minute 17 Once per second schedule.every().minute.at(":17").do(job) while True: # Get into schedule loop schedule.run_pending() time.sleep(1) Copy code
  It can be seen that ,api Very simple , Semantics is also very Clear . It can basically meet our needs ~ If we just have to do something regularly , I think this library is very human.
  Jenkins
  Compared with this tool, we all know something about it ~jenkins Is a relatively powerful software , It can get through git/svn, It also supports scheduled tasks, etc . There are plenty of plug-ins , such as
  mail , It's a complete set CI/CD Solution . Record the execution result of the task , It is also traceable . I guess the only drawback is , Introduced
  Additional systems . Maybe I'm just an automated testing framework , Now we have to build a set jenkins.
  If you have code changes -> Similar requirements for performing scheduled tasks , If you need a perfect authority control system , that jenkins It's going to be your choice .
  Celery
  What I know about this guy
  Not too much , but python+celery It seems to be a fixed collocation . I don't know if I understand :
  celery It's a task queue , You can make rules for task execution , Put it in the queue , There will be special consumers to help you perform these tasks .
  I have to mention here
  Persistence 了 ~
  The persistence of digression
  What is persistence ？ Generally speaking , It's instantaneous data （ For example, data in memory , It can't be kept forever ） Persist to persistent data （ For example, persistence to a database , Can be preserved for a long time ）
  We can
  data Put it in the file , You can also put it in the database （ It's actually on disk ） For persistent storage .
  The purpose of this : Data can be stored , I'll get these data next time . There's too much in it , I'm going to open another section later .
  We are Python The variables inside , These data are stored in
  Memory In the middle of , So we just schedule It is not supported
  Persistence Of .（ Or maybe I didn't study ）
  that jenkins Do you support it? ？ He is supportive , Would you , You build a project , There's some in there job Configuration of , When to execute , What did you do , The last save .
  The above is a persistent process .
  After you save the data , The task information of this project can be obtained next time , In other words, even if you restart jenkins,
  The task remains .
  explain
  Mission Itself as a piece of data , It has been preserved for a long time .
  ApScheduler
  "APScheduler It's a Python library , Allows you to schedule later Python Code , Only once or periodically . You can add new jobs or delete old jobs according to your needs . If you store work in a database , They will also restart the surviving scheduler and remain in state .
  Restart When the scheduler , It will run it offline 1 All jobs that should run when ."
  This is an excerpt from ApScheduler Introduction to the official website , Generally speaking, it supports the addition, deletion, modification and query of tasks ( Persistence ), It can also support Timing task Implementation . It's relatively light , Unlike celery So complicated , It's not like schedule So simple . Because we are not going to introduce
  jenkins, So in general ,ApScheduler It's the best choice for us to perform regular tasks .
  Because of the space , That's all for today ~
  In the next section, we will introduce in detail ApScheduler stay pity Application of sound in .
  author[Mil