sched The content of the module is very simple , Only one class is defined . It is used as a general event scheduling module .
class sched.scheduler(timefunc, delayfunc) This class defines a common interface for scheduling events , It needs to pass in two parameters externally ,timefunc Is a function that returns a time type number without parameters ( Commonly used, such as time In the module time),delayfunc It should be a call that requires a parameter 、 And timefunc The output of is compatible 、 And acts as a function of delaying multiple time units ( Commonly used as time Modular sleep).
Here is a list :
import sched, time
s = sched.scheduler(time.time, time.sleep) # Generate 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
# The four parameters are :
# Interval Events ( The specific value depends on delayfunc, This is seconds );
# priority ( Two events arrive at the same time );
# Trigger function ;
# Function parameter ;
s.enter(10, 1, print_time, ())
# function
s.run()
print time.time()
if __name__ == '__main__':
print_some_times()
See the output results , Partition 5 Execute the first event in seconds , Partition 10 Execute the second event in seconds :
1499259731.99
From print_time 1499259736.99
From print_time 1499259741.99
1499259741.99
In a multithreaded scenario , There will be thread safety issues ,run() Function blocks the main thread . Official recommended use threading.Timer Class substitution :
''' No one answers the problems encountered in learning ? Xiaobian created a Python Exchange of learning QQ Group :711312441 Looking for small partners who share the same aspiration , Help each other , There are also good video tutorials and PDF e-book ! '''
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) # Block main thread , Wait for the scheduler to finish executing , Then execute the following contents
print time.time()
if __name__ == '__main__':
print_some_times()
scheduler Object has the following methods or properties :
Add an event ,time The argument should be a relative to the one passed to the constructor timefunc Value types compatible with the return value of the function . Events arriving at the same time will follow priority Sequential execution .
Execution event is actually execution action(argument).argument Must be a containing action Sequence of parameters .
The return value is an event , It can be used to cancel the event later ( Please see the cancel()).
Schedule an event to delay delay Time units . Apart from time , The other parameters 、 Meaning and return value are the same as enterabs() The value of is the same . Inside enterabs Is used to be enter call .
Delete event from queue . If the event is not in the current queue , Then the method will run a ValueError.
Determines if the queue is empty .
Run all scheduled events . This function will wait ( Use the... Passed to the constructor delayfunc() function ), Then execute the event , Until there are no more scheduled events .
whatever action or delayfunc Can throw exceptions . In both cases , The scheduler will maintain a consistent state and propagate exceptions . If an exception is caused by action Caused by the , Will not continue to implement run().
Read-only property , Return a list of upcoming events ( Sort by arrival Events ), Every event has time、priority、action、argument Composed of namedtuple.