Today we are going to introducePython
Among the timed tasks, the main module used is thesched
, and of course in addition to that module there are others such as theApScheduler
But compared to thesched
module, the latter requires an additionalpip
command to download, which is a bit cumbersome. So in this tutorial we'll talk about thesched
Tutorial on the use of the module.
lit. have a small trial with a cow's knife
Let's look at the following case, the code is as follows
import sched import time def say_hello(name): print(f"Hello, world, {name}") scheduler = (, ) (5, 1, say_hello, ("Zhang San", )) ()
In the above code, the first step is to instantiate a timer, by using the following code
import sched scheduler = ()
Next, we'll go through theenter()
method to perform the operation of the timed task, where the parameters are the delay time, the priority of the task, and the specific execution function and the parameters in the execution function. Code such as the one above will be executed after a delay of 5 seconds.say_hello()
function (math.)
Of course, if the delays are equal, we can set the task execution priority to specify the order in which the functions and methods are run, for example, with the following code
import sched import time def say_hello(name): print(f"Hello, world, {name}") def say_hello_2(name): print(f"Hello, {name}") scheduler = (, ) (5, 2, say_hello, ("Zhang San", )) (5, 1, say_hello_2, ("Li Si.", )) ()
As in the above code, although the delays are all the same, thesay_hello()
method is clearly prioritized over thesay_hello_2()
method is lower, so the latter will be executed first.
Advanced Use
In addition to delaying the execution of a function, we can also make it repeat, specifically so, the code is as follows
import sched import time def say_hello(): print("Hello, world!") scheduler = (, ) def repeat_task(): (5, 1, say_hello, ()) (5, 1, repeat_task, ()) repeat_task() ()
Here we have created a newrepeat_task()
custom function that calls the()
method executes once every 5 seconds the previously definedsay_hello()
function (math.)
Execution of tasks at fixed times
Also we can make the task execute at a specified time, which is done using the()
method with the following code
import sched import time def say_hello(): print("Hello, world!") scheduler = (, ) # Tasks to be performed at specified times specific_time = () + 5 # Executed 5 seconds from now (specific_time, 1, say_hello, ()) ()
We pass in one of the parameters so that it executes the task at the specified time, i.e., 5 seconds from the present moment.
Perform multiple tasks
Here is still a call toenter()
method to run multiple tasks with the following code
import sched import time def task_one(): print("Task One - Hello, world!") def task_two(): print("Task Two - Hello, world!") scheduler = (, ) # Task I will be implemented in two seconds only (2, 1, task_one, ()) # Task two runs in five seconds (5, 1, task_two, ()) ()
Two functions are defined here, thetask_one
cap (a poem)task_two
The same logic is executed inside, printing out "Hello, world!" and thentask_one()
is executed two seconds later and thetask_two()
Then it is executed after 5 seconds, and both are executed with the same priority.
Perform different tasks with different priorities
This time we givetask_one()
cap (a poem)task_two()
Assign different priorities and look at the results of the execution as follows
import sched import time def task_one(): print("Task One - Hello, world!") def task_two(): print("Task Two - Hello, world!") scheduler = (, ) # Priority is 1 (2, 2, task_one, ()) # Priority is 2 (5, 1, task_two, ()) ()
output
Task One - Hello, world!
Task Two - Hello, world!
The above code will run after a two-second pausetask_one()
function, then pause for 3 seconds before executing thetask_two()
function (math.)
Timed tasks plus cancel method
We add a cancel method to the timed task with the following code
import sched import time def task_one(): print("Task One - Hello, world!") def task_two(): print("Task Two - Hello, world!") scheduler = (, ) # Task I will be implemented in two seconds only task_one_event = (2, 1, task_one, ()) # Task two runs in five seconds task_two_event = (5, 1, task_two, ()) # Cancel execution of task_one (task_one_event) ()
We're going to put in two seconds after the execution of thetask_one()
method is canceled out, and it ends up just executing thetask_two()
method, which also prints out "Task Two - Hello, world!"
Execute the backup program
Let's write a backup script that backs up the files at a fixed time each day, with the following code
import sched import time import shutil def backup_files(): source = 'path/files' destination = 'Path II' (source, destination) def schedule_backup(): # Create a new timer scheduler = (, ) # The backup procedure is performed at 1:00 a.m. every day. backup_time = ('01:00:00', '%H:%M:%S') backup_event = ((backup_time), 1, backup_files, ()) # Start a timed task () schedule_backup()
We do this byshutil
in the modulecopytree()
method to execute the copy file, and then at exactly 1:00 each day the
Implementation of procedures for the distribution of mail at regular intervals
Finally, let's execute the program that sends emails at regular intervals, with the following code
import sched import time import smtplib from import MIMEText def send_email(subject, message, from_addr, to_addr, smtp_server): # Message in the body of the e-mail email = MIMEText(message) email['Subject'] = subject email['From'] = from_addr email['To'] = to_addr # Send an e-mail with (smtp_server) as server: server.send_message(email) def send_scheduled_email(subject, message, from_addr, to_addr, smtp_server, scheduled_time): # Example of creating a timed task scheduler = (, ) # Timed emails (scheduled_time, 1, send_email, argument=(subject, message, from_addr, to_addr, smtp_server)) # Turn on the timer () subject = 'Test Email' message = 'This is a test email' from_addr = 'test@' to_addr = 'test@' smtp_server = '' scheduled_time = () + 60 # Execute the program after one minute send_scheduled_email(subject, message, from_addr, to_addr, smtp_server, scheduled_time)
to this article on the use of Python sched module to achieve the timed task of the article is introduced to this, more related Python sched timed task content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!