Python Standard Library sched Module Guide

  • 2020-06-07 04:49:03
  • OfStack

Event scheduling

The sched module content is simple, with only one class defined. It is used as a common event scheduling module.

class sched. scheduler (timefunc delayfunc) this class defines a common interface, scheduling events introduced into two parameters, it requires external timefunc is 1 returns the time type digital function (with no parameters (time in common use such as time module), delayfunc should be 1 to 1 parameter to invoke, compatible with timefunc output, and function is the function of multiple time delay units (commonly used such as time module sleep).

Here is one example:


import sched, time

s = 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 scheduling event 
# 4 The parameters are :
#  Interval event ( The specific value determines and delayfunc,  Here for seconds );
#  priority ( The two events are the same 1 The arrival of time );
#  Triggered function ;
#  Function parameters; 
s.enter(10, 1, print_time, ())

#  run 
s.run()
print time.time()

if __name__ == '__main__':
print_some_times()

To see the output, execute the first event after 5 seconds and the second event after 10 seconds:


1499259731.99
From print_time 1499259736.99
From print_time 1499259741.99
1499259741.99

In a multithreaded scenario, there are thread safety issues and the run() function blocks the main thread. The official recommendation is to use the ES27en.ES28en class instead:


import time
from threading import Timer

def 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) #  Blocking main thread , Wait for the scheduler to finish executing, then execute the rest 
print time.time()

if __name__ == '__main__':
print_some_times()

Scheduler object methods

The scheduler object has the following methods or properties:

scheduler.enterabs(time, priority, action, argument)

Adding an event, the time parameter should be a numeric type compatible with the return value of the timefunc function passed to the constructor. Events that arrive at the same time will be executed in the order priority.

The execution event is simply the execution of action(argument). argument must be a sequence containing the action parameter.

The return value is 1 event, which can be used later to cancel the event (see cancel()).

scheduler.enter(delay, priority, action, argument)

Schedule an event to delay delay time units. The parameters, meanings, and return values are the same as enterabs() except for the time. The internal enterabs is used to be called by enter.

scheduler.cancel(event)

Remove events from the queue. If the event is not in the current queue, the method runs 1 ValueError.

scheduler.empty()

Determine if the queue is empty.

scheduler.run()

Run all scheduled events. This function will wait (using the delayfunc() function passed to the constructor) and then execute the event until there are no more scheduled events.

Any action or delayfunc can throw an exception. In both cases, the scheduler will maintain a one-send state and propagate the exception. If an exception is caused by action, run() will not continue.

scheduler.queue

Read-only property that returns a list of incoming events (sorted by arrival event), each of which is an namedtuple consisting of time, priority, action, argument.


Related articles: