Python to implement scheduled tasks

Source: Internet
Author: User
This article describes how to implement scheduled tasks in Python. There are five methods available for reference. For more information, see. This article describes how to implement scheduled tasks in Python. There are five methods available for reference. For more information, see.

There are many ways to implement scheduled tasks in Python. Below are several

Loop sleep:

This is the easiest way to put the task to be executed in a loop, and then run it after sleep for a while. The disadvantage is that it is not easy to control and sleep is a blocking function.

Def timer (n): ''' run once every n seconds ''' while True: print time. strftime ('% Y-% m-% d % X', time. localtime () yourTask () # The time of the task to be executed. sleep (n)

Timer of threading:

The Timer in the threading module can help implement scheduled tasks and is non-blocking.

For example, to print helloworld in 3 seconds:

def printHello():   print "hello world"  Timer(3, printHello).start()

For example, helloworld is printed every 3 seconds:

def printHello():   print "Hello World"   t = Timer(2, printHello)   t.start()   if __name__ == "__main__":   printHello()

Use the sched module:

Sched is a scheduling (latency processing mechanism ).

#-*-Coding: UTF-8-*-# use sched to timing import time import OS import sched # Initialize the scheduler class of the sched module # The first parameter is a function that can return a timestamp, the second parameter can be blocked before the scheduled time reaches. Schedule = sched. scheduler (time. time, time. sleep) # function def execute_command (cmd, inc) triggered by periodic scheduling: ''' displays the connection status of the current computer ''' OS on the terminal. system (cmd) schedule. enter (inc, 0, execute_command, (cmd, inc) def main (cmd, inc = 60): # enter: interval event, priority (used for the execution of two events at the same time), called trigger function, # schedule for the parameter of the trigger function (in the form of tuple. enter (0, 0, execute_command, (cmd, inc) schedule. run () # view the network connection status every 60 seconds if _ name _ = '_ main _': main ("netstat-an", 60)

Use the timing framework APScheduler:

APScheduler is a Python scheduled task framework based on Quartz. Provides tasks based on the date, fixed time interval, and crontab type, and can persist tasks.

I have not tried this yet. I will try it later.

Use windows scheduled tasks:

Here, you can package the required Python program into an exe file, and then set the scheduled execution in windows.

The above is all the content of this article. I hope it will help you learn and support PHP.

For more articles about how to implement scheduled tasks in Python, refer to PHP!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.