home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pytho152.zip / emx / lib / python1.5 / lib-stdwin / WindowSched.py < prev   
Text File  |  2000-08-10  |  1KB  |  63 lines

  1. # Combine a real-time scheduling queue and stdwin event handling.
  2. # Keeps times in milliseconds.
  3.  
  4. import stdwin, stdwinq
  5. from stdwinevents import WE_TIMER
  6. import mainloop
  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.     msecs = int(msecs)
  15.     #
  16.     # Check for immediate stdwin event
  17.     #
  18.     event = stdwinq.pollevent()
  19.     if event:
  20.         mainloop.dispatch(event)
  21.         return
  22.     #
  23.     # Use sleep for very short delays or if there are no windows
  24.     #
  25.     if msecs < 100 or mainloop.countwindows() == 0:
  26.         if msecs > 0:
  27.             time.sleep(msecs * 0.001)
  28.         return
  29.     #
  30.     # Post a timer event on an arbitrary window and wait for it
  31.     #
  32.     window = mainloop.anywindow()
  33.     window.settimer(msecs/100)
  34.     event = stdwinq.getevent()
  35.     window.settimer(0)
  36.     if event[0] <> WE_TIMER:
  37.         mainloop.dispatch(event)
  38.  
  39. def millitimer():
  40.     return time.time() * 1000
  41.  
  42. q = sched.scheduler(millitimer, delayfunc)
  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():
  53.     return q.empty() and mainloop.countwindows() == 0
  54.  
  55. # Run until there is nothing left to do
  56. #
  57. def run():
  58.     while not empty():
  59.         if q.empty():
  60.             mainloop.dispatch(stdwinq.getevent())
  61.         else:
  62.             q.run()
  63.