home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Lib / stdwin / WindowSched.py < prev   
Text File  |  1994-02-18  |  1KB  |  62 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.     #
  15.     # Check for immediate stdwin event
  16.     #
  17.     event = stdwinq.pollevent()
  18.     if event:
  19.         mainloop.dispatch(event)
  20.         return
  21.     #
  22.     # Use sleep for very short delays or if there are no windows
  23.     #
  24.     if msecs < 100 or mainloop.countwindows() == 0:
  25.         if msecs > 0:
  26.             time.sleep(msecs * 0.001)
  27.         return
  28.     #
  29.     # Post a timer event on an arbitrary window and wait for it
  30.     #
  31.     window = mainloop.anywindow()
  32.     window.settimer(msecs/100)
  33.     event = stdwinq.getevent()
  34.     window.settimer(0)
  35.     if event[0] <> WE_TIMER:
  36.         mainloop.dispatch(event)
  37.  
  38. def millitimer():
  39.     return int(1000 * time.time())
  40.  
  41. q = sched.scheduler(millitimer, delayfunc)
  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():
  52.     return q.empty() and mainloop.countwindows() == 0
  53.  
  54. # Run until there is nothing left to do
  55. #
  56. def run():
  57.     while not empty():
  58.         if q.empty():
  59.             mainloop.dispatch(stdwinq.getevent())
  60.         else:
  61.             q.run()
  62.