blob: 67c3afbe22c2e1e6840eac11f1e7919d5d0f3d52 [file] [log] [blame]
Guido van Rossum2d844d11991-04-07 13:41:50 +00001# Combine a real-time scheduling queue and stdwin event handling.
2# Uses the millisecond timer.
3
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):
14 #
15 # Check for immediate stdwin event
16 #
Guido van Rossum0d3f4671991-08-16 13:22:53 +000017 event = stdwinq.pollevent()
Guido van Rossum2d844d11991-04-07 13:41:50 +000018 if event:
Guido van Rossum0d3f4671991-08-16 13:22:53 +000019 mainloop.dispatch(event)
Guido van Rossum2d844d11991-04-07 13:41:50 +000020 return
21 #
22 # Use millisleep for very short delays or if there are no windows
23 #
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000024 if msecs < 100 or mainloop.countwindows() == 0:
Guido van Rossum001fa6a1991-04-21 19:31:10 +000025 if msecs > 0:
26 time.millisleep(msecs)
Guido van Rossum2d844d11991-04-07 13:41:50 +000027 return
28 #
29 # Post a timer event on an arbitrary window and wait for it
30 #
Guido van Rossum0d3f4671991-08-16 13:22:53 +000031 window = mainloop.anywindow()
Guido van Rossum2d844d11991-04-07 13:41:50 +000032 window.settimer(msecs/100)
Guido van Rossum0d3f4671991-08-16 13:22:53 +000033 event = stdwinq.getevent()
Guido van Rossum2d844d11991-04-07 13:41:50 +000034 window.settimer(0)
35 if event[0] <> WE_TIMER:
Guido van Rossum0d3f4671991-08-16 13:22:53 +000036 mainloop.dispatch(event)
Guido van Rossum2d844d11991-04-07 13:41:50 +000037
38q = sched.scheduler().init(time.millitimer, delayfunc)
39
40# Export functions enter, enterabs and cancel just like a scheduler
41#
42enter = q.enter
43enterabs = q.enterabs
44cancel = q.cancel
45
46# Emptiness check must check both queues
47#
48def empty():
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000049 return q.empty() and mainloop.countwindows() == 0
Guido van Rossum2d844d11991-04-07 13:41:50 +000050
51# Run until there is nothing left to do
52#
53def run():
54 while not empty():
55 if q.empty():
Guido van Rossum0d3f4671991-08-16 13:22:53 +000056 mainloop.dispatch(stdwinq.getevent())
Guido van Rossum2d844d11991-04-07 13:41:50 +000057 else:
58 q.run()