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
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 , 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
If you just
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.
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 .
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 ）
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 .
Mission Itself as a piece of data , It has been preserved for a long time .
"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[Milo],Please bring the original link to reprint, thank you.
The sidebar is recommended
- [Python from introduction to mastery] (V) Python's built-in data types - sequences and strings. They have no girlfriend, not a nanny, and can only be used as dry goods
- Does Python have a, = operator?
- Go through the string common sense in Python
- Fanwai 4 Handling of mouse events and solutions to common problems in Python opencv
- Summary of common functions for processing strings in Python
- When writing Python scripts, be sure to add this
- Python web crawler - Fundamentals (1)
- Pandas handles duplicate values
- Python notes (23): regular module
- Python crawlers are slow? Concurrent programming to understand it
guess what you like
Parameter passing of Python function
Stroke tuple in Python
Talk about ordinary functions and higher-order functions in Python
[Python data acquisition] page image crawling and saving
[Python data collection] selenium automated test framework
Talk about function passing and other supplements in Python
Python programming simulation poker game
leetcode 160. Intersection of Two Linked Lists （python）
Python crawler actual combat, requests module, python to grab the beautiful wallpaper of a station
Fanwai 5 Detailed description of slider in Python opencv and solutions to common problems
- My friend's stock suffered a terrible loss. When I was angry, I crawled the latest data of securities with Python
- Python interface automation testing framework -- if you want to do well, you must first sharpen its tools
- Python multi thread crawling weather website pictures and saving
- How to convert pandas data to excel file
- Python series tutorials 122
- Python Complete Guide - printing data using pyspark
- Python Complete Guide -- tuple conversion array
- Stroke the list in python (top)
- Analysis of Python requests module
- Comments and variables in Python
- New statement match, the new version of Python is finally going to introduce switch case?
- Fanwai 6 Different operations for image details in Python opencv
- Python crawler native code learning (I)
- Python quantitative data warehouse building series 2: Python operation database
- Python code reading (Part 50): taking elements from list intervals
- Pyechart + pandas made word cloud pictures of 19 report documents
- [Python crawler] multithreaded daemon & join() blocking
- Python crawls cat pictures in batches to realize thousand image imaging
- Van * Python | simple crawling of a planet
- Input and output of Python practice
- Django ORM details - fields, attributes, operations
- Python web crawler - crawling cloud music review (3)
- Stroke list in python (bottom)
- What cat is the most popular? Python crawls the whole network of cat pictures. Which one is your favorite
- [algorithm learning] LCP 06 Take coins (Java / C / C + + / Python / go / trust)
- Python shows the progress of downloading files from requests
- Solve the problem that Django celery beat prompts that the database is incorrectly configured and does not support multiple databases
- Bamboolib: this will be one of the most useful Python libraries you've ever seen
- Python quantitative data warehouse construction 3: data drop library code encapsulation
- The source code and implementation of Django CSRF Middleware
- Python hashlib module
- The cover of Python 3 web crawler development (Second Edition) has been determined!
- The introduction method of executing Python source code or Python source code file (novice, please enter the old bird and turn left)
- [Python basics] explain Python basic functions in detail, including teaching and learning
- Python web crawler - crawling cloud music review (4)
- The first step of scientific research: create Python virtual environment on Linux server
- Writing nmap scanning tool in Python -- multithreaded version
- leetcode 2057. Smallest Index With Equal Value（python）
- Bamboolib: this will be one of the most useful Python libraries you've ever seen
- Python crawler actual combat, requests module, python realizes capturing a video barrage