wait() method of threading.Event() class

Mark Hammond mhammond at skippinet.com.au
Mon May 13 19:38:47 EDT 2002


Miranda Evans wrote:
> Using win32\Demos\timer_demo.py  as a framework for a solution to this
> issue led to 2 additional questions:
> 
> 1) are the last 2 arguments to MsgWaitForMultipleObjects() in
> timer_demo.py reversed? (I thought I saw a post that suggested this
> was the case, but haven't been able to find that post again.)

That appears to be true.  I have fixed it here.  Good catch ;)

> 2) given the nature of this particular application (wait for either an
> event from COM object to fire or for a timer to expire, whichever
> comes first) is it preferable to use two classes (one for the timer
> and one for the events from the COM object) or one class (combine
> timer and COM object event handler in one class)?

The best is probably to just wait for a single event with a timeout that 
corresponds to your "timer to expire" interval.

> 2 class example:
> # glork is timer
> class glork
>    # same __init__ and increments method as in timer_demo.py

Don't use a timer.  timer_demo was just an example of a MsgWaitFor* loop.

Something like:

# evHandle handles events from COM object
class evHandler
    def __init__(self):
       self.event  = win32event.CreateEvent(None, 0, 0, None)
    def OnSomeEvent(self):
       win32event.SetEvent(self.event)

def demo(delay=1000, stop=10):
    stop_time = time.time() + 60 # 60 seconds in the future.
    eh=evHandler()
    while 1:
       rc = win32event.MsgWaitForMultipleObjects(
                         (g.event,), # list of objects
                         0, # wait all
			1000,  # timeout
			win32event.QS_ALLEVENTS, # type of input
			)
      if rc == win32event.WAIT_OBJECT_0:
         # Event signalled - the COM object fired!
         break
      elif rc == win32event.WAIT_OBJECT_0+1:
         # Message waiting.
	pythoncom.PumpWaitingMessages()
      else:
         # This 1 second wait timed-out - see if time has expired.
	if time.time()>=stop_time:
            # time is up!
            break

Mark.




More information about the Python-list mailing list