Module Introduction
This module is mainly used for python task scheduling , using simple and friendly python syntax to regularly run python functions or some other call objects , this module is similar to the windows task schedule and linux crontab, are used to periodically execute a certain section of python script on the server .
Compared to linux crontab comparison:
- The schedule module supports periodic tasks in seconds, while crontab can only accomplish them in minutes.
- The schedule module is easy to manage when there are many tasks that need to be executed, embedded directly into the code, the crontab also needs to be set once per script, increasing the complexity.
- Lightweight, no dependencies
Module Installation
pip install schedule
# pip3 mounting
pip3 install schedule
Domestic installation to get the package may be very slow, you can use the domestic major acceleration to install the
# Tsinghua University source
pip install schedule -i /simple
# AliCloud source
pip install schedule -i /pypi/simple
# Douban source
pip instal lschedule -i /simple
Module Cases
# Load module
import schedule
# Create the required methods
def job(): print("output result")
print("output result")
# Set up the execution method
(). (job)
# Set up execution in a loop
while True: schedule.run_pending()
schedule.run_pending() # Run the timed task
(1) # Run the timed task 1S after the last one.
Code Explanation: The above code defines a job method to be executed once every minute, with a one-second interval between the completion of the execution and the execution of the next job method.
Module Parameter Usage
The schedule module provides a variety of timing methods, such as by the second, by the minute, by the hour, by the week, by the month, by the year, and so on, can be customized for different timed tasks to choose a specific timing method.
# Executed every second
(). (job)
# Executed every 30 seconds
(30). (job)
# Executed every minute
(). (job)
# Executed every 30 minutes
(30). (job)
# Executed every hour
(). (job)
# Executed every 2 hours
(2). (job)
# Executed once a day
(). (job)
# Execute once a day at 11:00
().day. at("11:00").do(job)
# Execute once a week
(). (job)
# Executed once a week on Wednesdays
(). (job)
For Monday through Sunday, use monday, tuesday, wednesday, thursday, friday, Saturday, sunday.
Single implementation
All of the above are repeated tasks, if you want the word to execute the task once, you can just RETURN it
# Load module
import schedule
# Create the required methods
def job(): print("output result")
print("output result")
return # Cancel the job
# Set up the execution method
(). (job)
# Set up execution in a loop
while True: schedule.run_pending()
schedule.run_pending() # Run the timed task
(1) # Run the timed task 1S after the last one.
Cancellation of assigned tasks
If there are multiple tasks and you want to stop the execution of one of them individually or you can set up to stop the execution of a task under certain circumstances.
# Load module
import schedule
# Create the required methods
def job(): print("output result")
print("output result")
return # Cancel the job
# Set up the execution method
(). (job).tag(job) # set the tag to job
(10). (job).tag(job10) # set label to job10
(20). (job).tag(job20) # Set the tag to job20
('job') # cancel the timed task labeled job
# Set up a loop
while True: schedule.run_pending()
schedule.run_pending() # Run the timed task
(1) # Run the timed task 1S after the last one.
Method Transfer Parameters
# Load module
import schedule
# Create the required methods
def job(name): print(name)
print(name)
# Set up the executing method
(). (job, name=job)
# Set up execution in a loop
while True: schedule.run_pending()
schedule.run_pending() # Run the timed task
(1) # Run the timed task for 1S after the last one.