This is the fourth day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021
Hello ~ I’m Milo! I am building an open source interface testing platform from 0 to 1, and I am also writing a complete tutorial corresponding to it. I hope you can support it. Welcome to pay attention to my public number test development pit goods, get the latest article tutorial!
Timing task
A scheduled task, as the name suggests, is a task that is executed regularly. It can be a bash command or a script file. Usually used when we need to do something at a particular time.
For example: performing automated regression tests on full-business scenario interfaces at 8pm every night, or stealing vegetables from your QQ farm every 20 minutes!
Python scheduled tasks
If it is a large or medium-sized company, there must be a corresponding platform for everyone to access. Such platforms are usually named or described as distributed task scheduling platforms. And then there’s Jenkins to meet our needs
Back when the blogger was “growing up”, the company didn’t use Jenkins or a big platform like this to access. How do I do timed tasks?
Take a look at this code:
import time
from datetime import datetime
def func() :
# This is the UI/interface automation method to perform scheduled tasks
pass
def main() :
while True:
Get the current time
now = datetime.now()
if now.hour == 20 and now.minute == 0:
# When it gets to 8:0pm, we start running automated tasks
print("Mission initiated.")
func()
time.sleep(60)
if __name__ == "__main__":
main()
Copy the code
I don’t know if anyone still performs automated tests regularly in this way, but since it’s 2021, let’s get ahead. So how do scheduled tasks work in Python? Then look at.
Several common scenarios
schedule
If you have a simple request as mentioned above, THEN I recommend using the Schedule library directly. This library I personally feel is an updated version of the elementary school version, with some extensions to my limited sleep task:
- Periodic execution of tasks is supported
- Execution times are more personal to read
- Also supports week X execution
- Install the scheduler
pip install schedule
Copy the code
- Examples from the official website
import schedule
import time
Define an execution method
def job() :
print("I'm working...")
# Execute job every 10 minutes
schedule.every(10).minutes.do(job)
Execute job every hour
schedule.every().hour.do(job)
Execute job at 10:30 every day
schedule.every().day.at("At 10:30").do(job)
Run this command once every Monday
schedule.every().monday.do(job)
# Every Wednesday at 13:15
schedule.every().wednesday.at("Anyone").do(job)
Execute at 17 seconds per minute
schedule.every().minute.at(", 17").do(job)
while True:
Enter the schedule loop
schedule.run_pending()
time.sleep(1)
Copy the code
As you can see, the API is very simple and the semantics are very clear. I think this library is very human if we just need to do something on a regular basis.
Jenkins
Jenkins is a more powerful software, it can get through git/ SVN, but also support scheduled tasks. With rich plug-ins, such as mail, is a complete CI/CD solution. Records of the execution results of tasks are also traceable. The only drawback to estimation is the introduction of additional systems. Maybe I’m just an automated testing framework, and now I’m building a Jenkins.
If you’re looking for code changes -> things like performing scheduled tasks, and you need a complete access control system, Jenkins is your go-to guy.
Celery
I don’t know much about this guy but python+celery seems to be a fixture. I don’t know if I understand this correctly:
Celery is a task queue, you can specify tasks execution rules, put them into the queue and there will be dedicated spyers to help you with tasks.
Here we have to mention persistence
Persistence of digressions
What is persistence? In layman’s terms, transient data (such as data in memory, which cannot be persisted) is persisted to persistent data (such as data persisted to a database, which can be persisted for a long time)
Data can be stored in a file or in a database (actually on disk) for persistence.
The goal: Data can be stored and retrieved next time. There’s so much in there, I’m going to do another section.
In Python, variables are stored in memory, so the schedule does not support persistence. (Or maybe I didn’t)
Does Jenkins support that? It supports, you want, you set up a project, there’s some job configuration, when it’s executed, what it’s done, and then save it.
This is a persistent process.
After you save the data, you will be able to retrieve the task information of the project next time, which means that even if you restart Jenkins, the task will still exist.
The task itself is stored as a piece of data.
ApScheduler
“APScheduler is a Python library that allows you to schedule Python code to be executed later, either once or periodically. You can add new jobs or delete old old jobs according to you. If you store jobs in the database, they will also restart the living scheduler and keep the state. When the scheduler is restarted, it will run all the jobs it should have run offline 1.
This is an introduction from ApScheduler’s official website. In general, it supports adding, deleting, changing and searching tasks (persistence), and also supports the execution of scheduled tasks well. It is relatively lightweight, not as complex as celery and not as rudimentary as Schedule. Since we’re not going to introduce Jenkins, ApScheduler is generally the best choice for scheduled tasks.
Due to space reasons, today’s content is introduced here ~
In the next section we’ll look at the use of ApScheduler in pity in more detail.