Mercurial > cpython-lru_cache-weakref
annotate Lib/stdwin/WindowSched.py @ 5817:48b94de85bf3 legacy-trunk
Extensive changes by AMK.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 17 Jul 1997 16:29:42 +0000 |
| parents | 2dd285794d67 |
| children |
| 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): | |
|
4707
2dd285794d67
Fixed conversion between seconds and milliseconds.
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
14 msecs = int(msecs) |
| 195 | 15 # |
| 16 # Check for immediate stdwin event | |
| 17 # | |
|
355
8a94ccc4c914
Used mainloop instead of WindowParent functions
Guido van Rossum <guido@python.org>
parents:
214
diff
changeset
|
18 event = stdwinq.pollevent() |
| 195 | 19 if event: |
|
355
8a94ccc4c914
Used mainloop instead of WindowParent functions
Guido van Rossum <guido@python.org>
parents:
214
diff
changeset
|
20 mainloop.dispatch(event) |
| 195 | 21 return |
| 22 # | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
23 # Use sleep for very short delays or if there are no windows |
| 195 | 24 # |
| 559 | 25 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
|
26 if msecs > 0: |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
27 time.sleep(msecs * 0.001) |
| 195 | 28 return |
| 29 # | |
| 30 # Post a timer event on an arbitrary window and wait for it | |
| 31 # | |
|
355
8a94ccc4c914
Used mainloop instead of WindowParent functions
Guido van Rossum <guido@python.org>
parents:
214
diff
changeset
|
32 window = mainloop.anywindow() |
| 195 | 33 window.settimer(msecs/100) |
|
355
8a94ccc4c914
Used mainloop instead of WindowParent functions
Guido van Rossum <guido@python.org>
parents:
214
diff
changeset
|
34 event = stdwinq.getevent() |
| 195 | 35 window.settimer(0) |
| 36 if event[0] <> WE_TIMER: | |
|
355
8a94ccc4c914
Used mainloop instead of WindowParent functions
Guido van Rossum <guido@python.org>
parents:
214
diff
changeset
|
37 mainloop.dispatch(event) |
| 195 | 38 |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
39 def millitimer(): |
|
4707
2dd285794d67
Fixed conversion between seconds and milliseconds.
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
40 return time.time() * 1000 |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
41 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
42 q = sched.scheduler(millitimer, delayfunc) |
| 195 | 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(): | |
| 559 | 53 return q.empty() and mainloop.countwindows() == 0 |
| 195 | 54 |
| 55 # Run until there is nothing left to do | |
| 56 # | |
| 57 def run(): | |
| 58 while not empty(): | |
| 59 if q.empty(): | |
|
355
8a94ccc4c914
Used mainloop instead of WindowParent functions
Guido van Rossum <guido@python.org>
parents:
214
diff
changeset
|
60 mainloop.dispatch(stdwinq.getevent()) |
| 195 | 61 else: |
| 62 q.run() |
