Python standard library notes (5)-sched module, pythonsched
Event Scheduling
sched
The module content is very simple and only defines one class. It is used as the most common event scheduling module.
class sched.scheduler(timefunc, delayfunc)
This class defines a common interface for scheduling events. It requires two parameters to be passed in externally,timefunc
Is a function that returns time numbers without parameters (such as the time in the time module ),delayfunc
It should be a function that requires a parameter to call, be compatible with timefunc output, and act as a function that delays multiple time units (such as sleep of the time module ).
The following is a column:
Import sched, times = sched. scheduler (time. time, time. sleep) # generate the scheduler def print_time (): print "From print_time", time. time () def print_some_times (): print time. time () s. enter (5, 1, print_time, () # Add a scheduling event # The four parameters are: # The interval event (the specific value is determined by delayfunc, here is the second ); # priority (when two events arrive at the same time); # triggered functions; # function parameters; s. enter (10, 1, print_time, () # Run s. run () print time. time () if _ name _ = '_ main _': print_some_times ()
The output result shows that the first event is executed every five seconds, and the second event is executed every 10 seconds:
1499259731.99From print_time 1499259736.99From print_time 1499259741.991499259741.99
In multi-threaded scenarios, thread security issues occur. The run () function blocks the main thread. Officially recommendedthreading.Timer
Class substitution:
Import timefrom threading import Timerdef print_time (): print "From print_time", time. time () def print_some_times (): print time. time () Timer (5, print_time ,()). start () Timer (10, print_time ,()). start () time. sleep (11) # block the main thread, wait until the scheduling program is executed, and then execute the following content print time. time () if _ name _ = '_ main _': print_some_times ()
Scheduler object Method
The scheduler object has the following methods or attributes:
- Schedity. enterabs (time, priority, action, argument)
Add an event,time
The parameter should be the same as the one passed to the constructor.timefunc
A value type that is compatible with the return value of a function. Events that arrive at the same time will followpriority
Sequential execution.
The execution event is actually the executionaction(argument)
. Argument must beaction
Parameter sequence.
The return value is an event that can be used to cancel an event later (seecancel()
).
- Schedity. enter (delay, priority, action, argument)
Schedule an event to delaydelay
Time unit. In addition to time, other parameters, meanings, and returned valuesenterabs()
. Actually internalenterabs
Is usedenter
Call.
Delete an event from the queue. If the event is not an event in the current queue, this method runsValueError
.
Determines whether the queue is empty.
Run all scheduled events. This function will wait (usingdelayfunc()
Function), and then execute the event until there is no scheduled event.
Anyaction
Ordelayfunc
Can cause exceptions. In both cases, the scheduler will maintain a consistent state and spread exceptions. If an exception is causedaction
The execution will not continue.run()
.
Read-only attribute, returns a list of events to be reached (sorted by arrival events), each event hastime
,priority
,action
,argument
Composednamedtuple
.