Home  >  Article  >  Backend Development  >  How to use Python timer

How to use Python timer

王林
王林forward
2023-05-12 16:43:101396browse

Here we develop a print_datetime function to print the current time, and also use the print_time function as a task that we need to keep executing.

# Importing the datetime module.
import datetime


def print_time(message=None):
    """
    It prints the current time, optionally preceded by a message.

    :param message: The message to print
    """
    print(message, datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))

Then, we prepare the scheduled task module apscheduler that needs to be used. You can choose the pip method to install it. This is the method I have always used.

pip install apscheduler

# Importing the BlockingScheduler class from the apscheduler.schedulers.blocking module.
from apscheduler.schedulers.blocking import BlockingScheduler

At this point, we can execute the business function that needs to be kept in execution state, that is, the print_datetime function here as a scheduled task.

In this way, we don’t need to use the while True infinite loop sleep method to keep the task in running status.

# Creating a scheduler object.
scheduler = BlockingScheduler()

# Adding a job to the scheduler.
scheduler.add_job(func=print_time, args=('时间打印定时任务',), trigger='cron', second='*/1')  # 每秒执行

# Starting the scheduler in a separate thread.
scheduler.start()

Finally, just start the current .py file to directly execute the scheduled task. The running effect is as follows.

Time printing scheduled task 2023-02-26 13:52:52
Time printing scheduled task 2023-02-26 13:52:53
Time printing scheduled task 2023-02 -26 13:52:54
Time printing scheduled task 2023-02-26 13:52:55
Time printing scheduled task 2023-02-26 13:52:56
Time printing scheduled task 2023- 02-26 13:52:57

Of course, as the framework apscheduler for scheduled tasks, he also has many skills. For example: execution in more complex cycles, execution within a limited time, single point execution, etc.

The following is the execution method of the more common apscheduler scheduled tasks that I have listed for your reference and valuable opinions.

scheduler.add_job(func=print_time, args=('任务只执行一次,在下一次的时间执行',),
                  next_run_time=datetime.datetime.now() + datetime.timedelta(seconds=60))

scheduler.add_job(func=print_time, args=('时间打印定时任务',), trigger='interval', seconds=5)  # 每5秒执行一次
scheduler.add_job(func=print_time, args=('时间打印定时任务',), trigger='interval', minutes=2)  # 每2分钟执行一次
scheduler.add_job(func=print_time, args=('时间打印定时任务',), trigger='interval', hours=1)  # 每1小时执行一次

scheduler.add_job(func=print_time, args=('时间打印定时任务',), trigger='cron', minute='*', second='1')  # 每分钟执行一次
scheduler.add_job(func=print_time, args=('时间打印定时任务',), trigger='cron', hour='*', minute='0',
                  second='0')  # 每小时执行一次

scheduler.add_job(func=print_time, args=('时间打印定时任务',), trigger='cron', hour='20', minute='0',
                  second='0')  # 每天20:00执行一次
scheduler.add_job(func=print_time, args=('时间打印定时任务',), trigger='cron', hour='21')  # 每天21:00执行一次

The above is the detailed content of How to use Python timer. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete