we often need perform a task on a timed basis , we have a powerful crontab under Linux.
We often need to perform a task on a regular basis, there is a strong crontab under Linux, and in Python in addition to the third-party module, Python has its own sched module and timer class for completing the cycle task. The timer class is in the threading module.
The following main description of the use of the Sched module
Scheduler.scheduler (Timefunc, Delayfunc)
Scheduler has the following methods and properties:
Scheduler.enterabs (time,priority,action,argument) generates a dispatch event whose return value can be used to cancel the scheduled event. Events executed at the same time will be processed in their order of precedence
Scheduler.enter (delay,priority,action,argument) generates a dispatch event that returns the same value as Enterabs
Scheduler.cancel (event) cancels a dispatch event with the return value of the parameter enterabs,enter.
Scheduler.empty () Determines whether the scheduled event queue is empty and Null returns True
Scheduler.run () runs all scheduled events, blocking waits for completion. This method uses the Delayfunc blocking in the constructor method to block the next event, and then executes it until all the scheduled events in the dispatch queue are completed. The Cancel method and the Run method need not be in one thread if the same thread needs to cancel the event before run
Scheduler.queue Scheduling Queue
The Sched module is a deferred dispatch processing module that must be written to a dispatch task (called method) each time a task is executed.
Use the following:
To generate a scheduler:
Import time,sched
S=sched.scheduler (Time.time,time.sleep)
Note: The first parameter must be a time function without parameters, and must return a number, for example, this example returns a timestamp, and the second parameter is a blocking function, which takes a parameter that is compatible with the output of the time function.
To add the first scheduled event:
To add a schedule event, there are 2 enter and Enterabs, with enter as an example:
Def print_time ():
Print "From Print_time", Time.time ()
S.enter (5,1,print_time, ())
3. Implementation
S.run ()
It is important to note that the Sched module is not circular, and once the dispatch is executed, it is finished, and if you want to do it again, enter again. You do not need to execute run again after enter. If you repeatedly call run in a method, it causes an exception when the maximum recursion depth is exceeded and terminates the task execution .
#! /usr/bin/env python#coding=utf-8import time, os, sched # The first parameter determines the time of the task, returns the number of seconds from a specific time to the present # the second parameter measures time in some artificial way schedule = Sched.scheduler (Time.time, time.sleep) def run_command (cmd, inc): # Schedule Inc Second to run itself again, that is, Cycle Run schedule.enter (inc, 0, run_command, (Cmd, inc)) os.system (cmd) def timming_exe (cmd, inc = 60): # enter is used to schedule the occurrence of an event, starting from now on the nth second start schedule.enter (inc, 0, run_command, (cmd, inc)) # runs continuously until the scheduled time queue becomes empty schedule.run () Print ("SHOW TIME AFTER 10&NBsp;seconds: ") if os.name == " NT ": timming_exe (" Echo %time% ",  10) elif os.name== "POSIX": timming_exe ("echo ' Date '", 10)
This article is from the "Bin Lin Xia" blog, please be sure to keep this source http://cbotz.blog.51cto.com/4039348/1795586
Use the Sched library to complete a periodic scheduled task