0
scheduler.run
の呼び出し後にscheduler.enter
を使用してイベントをスケジュールすると、これらのイベントは、スケジューラの実行前に入力されたイベントが少なくとも1つ発生するまで発生しません。これは意図されていますか?あなたは、スケジューラを起動するとpython3 sched:実行後のイベントのスケジュール
import sched
import threading
import time
now = time.time()
scheduler = sched.scheduler(time.time, time.sleep)
def fire(name):
print('EVENT:', name, time.time() - now)
def start():
t = threading.Thread(target=scheduler.run)
t.start()
###################################
# when scheduling all before start() everything works as expected.
# scheduler.enter(4, 1, fire, ('D',))
# scheduler.enter(3, 1, fire, ('C',))
# scheduler.enter(2, 1, fire, ('B',))
# scheduler.enter(1, 1, fire, ('A',))
# start()
#
# EVENT: A 1.0
# EVENT: B 2.0
# EVENT: C 3.0
# EVENT: D 4.0
###################################
# here I'd expect event 'A' to fire after one second. But it is
# not fired until at least one of the events scheduled before run()
# is fired.
# scheduler.enter(2, 1, fire, ('B',))
# scheduler.enter(4, 1, fire, ('D',))
# start()
# scheduler.enter(1, 1, fire, ('A',))
# scheduler.enter(3, 1, fire, ('C',))
#
# EVENT: A 2.005021572113037
# EVENT: B 2.005021572113037
# EVENT: C 3.0050315856933594
# EVENT: D 4.0050413608551025
###################################
# starting before any event is scheduled is not possible,
# because run() returns directly.
#
# start()
# scheduler.enter(3, 1, fire, ('C',))
# scheduler.enter(2, 1, fire, ('B',))
# scheduler.enter(1, 1, fire, ('A',))
#
# -- no events fired.