Mercurial > cpython-lru_cache-weakref
annotate Lib/stdwin/WindowSched.py @ 4566:6403c94de50d legacy-trunk
Added safeguard against failure in __del__.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 27 Nov 1996 19:39:58 +0000 |
| parents | 1940892d2fe3 |
| children | 2dd285794d67 |
| rev | line source |
|---|---|
| 195 | 1 # Combine a real-time scheduling queue and stdwin event handling. |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
2 # Keeps times in milliseconds. |
| 195 | 3 |
|
355
8a94ccc4c914
Used mainloop instead of WindowParent functions
Guido van Rossum <guido@python.org>
parents:
214
diff
changeset
|
4 import stdwin, stdwinq |
| 195 | 5 from stdwinevents import WE_TIMER |
|
355
8a94ccc4c914
Used mainloop instead of WindowParent functions
Guido van Rossum <guido@python.org>
parents:
214
diff
changeset
|
6 import mainloop |
| 195 | 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): | |
| 14 # | |
| 15 # Check for immediate stdwin event | |
| 16 # | |
|
355
8a94ccc4c914
Used mainloop instead of WindowParent functions
Guido van Rossum <guido@python.org>
parents:
214
diff
changeset
|
17 event = stdwinq.pollevent() |
| 195 | 18 if event: |
|
355
8a94ccc4c914
Used mainloop instead of WindowParent functions
Guido van Rossum <guido@python.org>
parents:
214
diff
changeset
|
19 mainloop.dispatch(event) |
| 195 | 20 return |
| 21 # | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
22 # Use sleep for very short delays or if there are no windows |
| 195 | 23 # |
| 559 | 24 if msecs < 100 or mainloop.countwindows() == 0: |
|
214
ed0311b8e741
Don't sleep if msecs <= 0.
Guido van Rossum <guido@python.org>
parents:
195
diff
changeset
|
25 if msecs > 0: |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
26 time.sleep(msecs * 0.001) |
| 195 | 27 return |
| 28 # | |
| 29 # Post a timer event on an arbitrary window and wait for it | |
| 30 # | |
|
355
8a94ccc4c914
Used mainloop instead of WindowParent functions
Guido van Rossum <guido@python.org>
parents:
214
diff
changeset
|
31 window = mainloop.anywindow() |
| 195 | 32 window.settimer(msecs/100) |
|
355
8a94ccc4c914
Used mainloop instead of WindowParent functions
Guido van Rossum <guido@python.org>
parents:
214
diff
changeset
|
33 event = stdwinq.getevent() |
| 195 | 34 window.settimer(0) |
| 35 if event[0] <> WE_TIMER: | |
|
355
8a94ccc4c914
Used mainloop instead of WindowParent functions
Guido van Rossum <guido@python.org>
parents:
214
diff
changeset
|
36 mainloop.dispatch(event) |
| 195 | 37 |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
38 def millitimer(): |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
39 return int(1000 * time.time()) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
40 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
41 q = sched.scheduler(millitimer, delayfunc) |
| 195 | 42 |
| 43 # Export functions enter, enterabs and cancel just like a scheduler | |
| 44 # | |
| 45 enter = q.enter | |
| 46 enterabs = q.enterabs | |
| 47 cancel = q.cancel | |
| 48 | |
| 49 # Emptiness check must check both queues | |
| 50 # | |
| 51 def empty(): | |
| 559 | 52 return q.empty() and mainloop.countwindows() == 0 |
| 195 | 53 |
| 54 # Run until there is nothing left to do | |
| 55 # | |
| 56 def run(): | |
| 57 while not empty(): | |
| 58 if q.empty(): | |
|
355
8a94ccc4c914
Used mainloop instead of WindowParent functions
Guido van Rossum <guido@python.org>
parents:
214
diff
changeset
|
59 mainloop.dispatch(stdwinq.getevent()) |
| 195 | 60 else: |
| 61 q.run() |
