sched — 定时消息调度器
优质
小牛编辑
129浏览
2023-12-01
Running Events With a Delay
# sched_basic.py
import sched
import time
scheduler = sched.scheduler(time.time, time.sleep)
def print_event(name, start):
now = time.time()
elapsed = int(now - start)
print('EVENT: {} elapsed={} name={}'.format(
time.ctime(now), elapsed, name))
start = time.time()
print('START:', time.ctime(start))
scheduler.enter(2, 1, print_event, ('first', start))
scheduler.enter(3, 1, print_event, ('second', start))
scheduler.run()
Overlapping Events
# sched_overlap.py
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, ('second',))
scheduler.run()
Event Priorities
# sched_priority.py
import sched
import time
scheduler = sched.scheduler(time.time, time.sleep)
def print_event(name):
print('EVENT:', time.ctime(time.time()), name)
now = time.time()
print('START:', time.ctime(now))
scheduler.enterabs(now + 2, 2, print_event, ('first',))
scheduler.enterabs(now + 2, 1, print_event, ('second',))
scheduler.run()
Canceling Events
# sched_cancel.py
import sched
import threading
import time
scheduler = sched.scheduler(time.time, time.sleep)
# Set up a global to be modified by the threads
counter = 0
def increment_counter(name):
global counter
print('EVENT:', time.ctime(time.time()), name)
counter += 1
print('NOW:', counter)
print('START:', time.ctime(time.time()))
e1 = scheduler.enter(2, 1, increment_counter, ('E1',))
e2 = scheduler.enter(3, 1, increment_counter, ('E2',))
# Start a thread to run the events
t = threading.Thread(target=scheduler.run)
t.start()
# Back in the main thread, cancel the first scheduled event.
scheduler.cancel(e1)
# Wait for the scheduler to finish running in the thread
t.join()
print('FINAL:', counter)