Guido van Rossum | 2d844d1 | 1991-04-07 13:41:50 +0000 | [diff] [blame] | 1 | # Combine a real-time scheduling queue and stdwin event handling. |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2 | # Keeps times in milliseconds. |
Guido van Rossum | 2d844d1 | 1991-04-07 13:41:50 +0000 | [diff] [blame] | 3 | |
Guido van Rossum | 0d3f467 | 1991-08-16 13:22:53 +0000 | [diff] [blame] | 4 | import stdwin, stdwinq |
Guido van Rossum | 2d844d1 | 1991-04-07 13:41:50 +0000 | [diff] [blame] | 5 | from stdwinevents import WE_TIMER |
Guido van Rossum | 0d3f467 | 1991-08-16 13:22:53 +0000 | [diff] [blame] | 6 | import mainloop |
Guido van Rossum | 2d844d1 | 1991-04-07 13:41:50 +0000 | [diff] [blame] | 7 | import sched |
| 8 | import 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 | # |
| 13 | def delayfunc(msecs): |
Guido van Rossum | 0caa7ec | 1996-12-12 23:42:20 +0000 | [diff] [blame] | 14 | msecs = int(msecs) |
Guido van Rossum | 2d844d1 | 1991-04-07 13:41:50 +0000 | [diff] [blame] | 15 | # |
| 16 | # Check for immediate stdwin event |
| 17 | # |
Guido van Rossum | 0d3f467 | 1991-08-16 13:22:53 +0000 | [diff] [blame] | 18 | event = stdwinq.pollevent() |
Guido van Rossum | 2d844d1 | 1991-04-07 13:41:50 +0000 | [diff] [blame] | 19 | if event: |
Guido van Rossum | 0d3f467 | 1991-08-16 13:22:53 +0000 | [diff] [blame] | 20 | mainloop.dispatch(event) |
Guido van Rossum | 2d844d1 | 1991-04-07 13:41:50 +0000 | [diff] [blame] | 21 | return |
| 22 | # |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 23 | # Use sleep for very short delays or if there are no windows |
Guido van Rossum | 2d844d1 | 1991-04-07 13:41:50 +0000 | [diff] [blame] | 24 | # |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 25 | if msecs < 100 or mainloop.countwindows() == 0: |
Guido van Rossum | 001fa6a | 1991-04-21 19:31:10 +0000 | [diff] [blame] | 26 | if msecs > 0: |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 27 | time.sleep(msecs * 0.001) |
Guido van Rossum | 2d844d1 | 1991-04-07 13:41:50 +0000 | [diff] [blame] | 28 | return |
| 29 | # |
| 30 | # Post a timer event on an arbitrary window and wait for it |
| 31 | # |
Guido van Rossum | 0d3f467 | 1991-08-16 13:22:53 +0000 | [diff] [blame] | 32 | window = mainloop.anywindow() |
Guido van Rossum | 2d844d1 | 1991-04-07 13:41:50 +0000 | [diff] [blame] | 33 | window.settimer(msecs/100) |
Guido van Rossum | 0d3f467 | 1991-08-16 13:22:53 +0000 | [diff] [blame] | 34 | event = stdwinq.getevent() |
Guido van Rossum | 2d844d1 | 1991-04-07 13:41:50 +0000 | [diff] [blame] | 35 | window.settimer(0) |
| 36 | if event[0] <> WE_TIMER: |
Guido van Rossum | 0d3f467 | 1991-08-16 13:22:53 +0000 | [diff] [blame] | 37 | mainloop.dispatch(event) |
Guido van Rossum | 2d844d1 | 1991-04-07 13:41:50 +0000 | [diff] [blame] | 38 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 39 | def millitimer(): |
Guido van Rossum | 0caa7ec | 1996-12-12 23:42:20 +0000 | [diff] [blame] | 40 | return time.time() * 1000 |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 41 | |
| 42 | q = sched.scheduler(millitimer, delayfunc) |
Guido van Rossum | 2d844d1 | 1991-04-07 13:41:50 +0000 | [diff] [blame] | 43 | |
| 44 | # Export functions enter, enterabs and cancel just like a scheduler |
| 45 | # |
| 46 | enter = q.enter |
| 47 | enterabs = q.enterabs |
| 48 | cancel = q.cancel |
| 49 | |
| 50 | # Emptiness check must check both queues |
| 51 | # |
| 52 | def empty(): |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 53 | return q.empty() and mainloop.countwindows() == 0 |
Guido van Rossum | 2d844d1 | 1991-04-07 13:41:50 +0000 | [diff] [blame] | 54 | |
| 55 | # Run until there is nothing left to do |
| 56 | # |
| 57 | def run(): |
| 58 | while not empty(): |
| 59 | if q.empty(): |
Guido van Rossum | 0d3f467 | 1991-08-16 13:22:53 +0000 | [diff] [blame] | 60 | mainloop.dispatch(stdwinq.getevent()) |
Guido van Rossum | 2d844d1 | 1991-04-07 13:41:50 +0000 | [diff] [blame] | 61 | else: |
| 62 | q.run() |