Chapter XIV: Application Builder module -sched: timer event scheduler - overlapping events

14.11.2 overlapping events
run () call blocks until all events have been processed. Each event run in the same thread, so if an event takes a long event to run, and beyond the delay between the event, then there will be overlap. Overlap can be solved by delaying the back of the event. Such events are not lost, but some events may call later than its scheduled time. In the following example, long_event () call sleep sleep, but can also be done a long time in computing or block I / O, so that can be easily delayed.

import sched
import time

scheduler = sched.scheduler(time.time,time.sleep)


def long_event(name):
    print('BEGIN EVENT :',time.ctime(time.time()),name)
    time.sleep(2)
    print('FINISH EVENT:',time.ctime(time.time()),name)

print('START:',time.ctime(time.time()))
scheduler.enter(2,1,long_event,('first',))
scheduler.enter(3,1,long_event,('seconod',))

scheduler.run()

The result is the first event will run immediately upon completion of the second event, the first event because of the time it takes long enough to make the clock more than a second event desired start time.
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_43193719/article/details/94654219