blob: 119a41feeb8fe248e7f694a5229f3a8f8b0dc5bb [file] [log] [blame]
Guido van Rossum2d844d11991-04-07 13:41:50 +00001# Combine a real-time scheduling queue and stdwin event handling.
Guido van Rossumb6775db1994-08-01 11:34:53 +00002# Keeps times in milliseconds.
Guido van Rossum2d844d11991-04-07 13:41:50 +00003
Guido van Rossum0d3f4671991-08-16 13:22:53 +00004import stdwin, stdwinq
Guido van Rossum2d844d11991-04-07 13:41:50 +00005from stdwinevents import WE_TIMER
Guido van Rossum0d3f4671991-08-16 13:22:53 +00006import mainloop
Guido van Rossum2d844d11991-04-07 13:41:50 +00007import sched
8import time
9
10# Delay function called by the scheduler when it has nothing to do.
11# Return immediately when something is done, or when the delay is up.
12#
13def delayfunc(msecs):
Guido van Rossum0caa7ec1996-12-12 23:42:20 +000014 msecs = int(msecs)
Guido van Rossum2d844d11991-04-07 13:41:50 +000015 #
16 # Check for immediate stdwin event
17 #
Guido van Rossum0d3f4671991-08-16 13:22:53 +000018 event = stdwinq.pollevent()
Guido van Rossum2d844d11991-04-07 13:41:50 +000019 if event:
Guido van Rossum0d3f4671991-08-16 13:22:53 +000020 mainloop.dispatch(event)
Guido van Rossum2d844d11991-04-07 13:41:50 +000021 return
22 #
Guido van Rossumb6775db1994-08-01 11:34:53 +000023 # Use sleep for very short delays or if there are no windows
Guido van Rossum2d844d11991-04-07 13:41:50 +000024 #
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000025 if msecs < 100 or mainloop.countwindows() == 0:
Guido van Rossum001fa6a1991-04-21 19:31:10 +000026 if msecs > 0:
Guido van Rossumb6775db1994-08-01 11:34:53 +000027 time.sleep(msecs * 0.001)
Guido van Rossum2d844d11991-04-07 13:41:50 +000028 return
29 #
30 # Post a timer event on an arbitrary window and wait for it
31 #
Guido van Rossum0d3f4671991-08-16 13:22:53 +000032 window = mainloop.anywindow()
Guido van Rossum2d844d11991-04-07 13:41:50 +000033 window.settimer(msecs/100)
Guido van Rossum0d3f4671991-08-16 13:22:53 +000034 event = stdwinq.getevent()
Guido van Rossum2d844d11991-04-07 13:41:50 +000035 window.settimer(0)
36 if event[0] <> WE_TIMER:
Guido van Rossum0d3f4671991-08-16 13:22:53 +000037 mainloop.dispatch(event)
Guido van Rossum2d844d11991-04-07 13:41:50 +000038
Guido van Rossumb6775db1994-08-01 11:34:53 +000039def millitimer():
Guido van Rossum0caa7ec1996-12-12 23:42:20 +000040 return time.time() * 1000
Guido van Rossumb6775db1994-08-01 11:34:53 +000041
42q = sched.scheduler(millitimer, delayfunc)
Guido van Rossum2d844d11991-04-07 13:41:50 +000043
44# Export functions enter, enterabs and cancel just like a scheduler
45#
46enter = q.enter
47enterabs = q.enterabs
48cancel = q.cancel
49
50# Emptiness check must check both queues
51#
52def empty():
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000053 return q.empty() and mainloop.countwindows() == 0
Guido van Rossum2d844d11991-04-07 13:41:50 +000054
55# Run until there is nothing left to do
56#
57def run():
58 while not empty():
59 if q.empty():
Guido van Rossum0d3f4671991-08-16 13:22:53 +000060 mainloop.dispatch(stdwinq.getevent())
Guido van Rossum2d844d11991-04-07 13:41:50 +000061 else:
62 q.run()