wait() method of threading.Event() class

Miranda Evans mirandacascade at yahoo.com
Mon May 13 18:22:31 EDT 2002


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.)

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)?

2 class example:
# glork is timer
class glork
   # same __init__ and increments method as in timer_demo.py
# 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):
   g=glork(delay, stop)
   eh=evHandler()
   # same code as in timer_demo.py except in while loop

      rc = win32event.MsgWaitForMultipleObjects((g.event,
eh.event),0,500,
           win32event.QS_ALLEVENTS) 
      #WAIT_OBJECT_0 to test for g.event
      #WAIT_OBJECT_0+1 to test for eh.event

Or

one class example

class glork:
   # same __init__() and increment() methods as in timer_demo.py
   def OnSomeEvent(self):
      win32event.SetEvent(self.event)

# demo() method same as in timer_demo.py; where first arg to 
# MsgWaitForMultiple objects is single element tuple (g.event,)

Since this application wants to quit based on a whichever comes first
rule, I thought it might be okay to combine the two things (timer and
COM event notification) in one class, but wasn't sure if either:
 - that's bad design 
      or
 - there are bad side effects to that design   

Mark Hammond <mhammond at skippinet.com.au> wrote in message news:<3CD9B0A7.8010504 at skippinet.com.au>...
> Miranda Evans wrote:
> > Using: python 2.2, PythonWin environment, Windows 2000 O/S.
> > 
> > Attempting to have a script handle the possibility that an event from
> > a COM object might fire within x seconds.  Wanted to have the script
> > wait until either the event fired or x seconds elapsed (whichever
> > comes first.) Thought this might be a good candidate for the
> > threading.Event class.
> > 
> > test.py:
> > 
> > import win32com.client
> > import threading
> > testev = threading.Event()
> > class xlEvents:
> >    def OnSheetBeforeDoubleClick(self, sh, Target, cancel):
> >       testev.set()
> > 
> > def driver():
> >    xl = win32com.client.DispatchWithEvents("Excel.Application",
> > xlEvents)
> >    xl.Visible=1
> >    xl.Workbooks.Add()
> >    # wait up to 30 seconds (assumes wait ends if testev is set in < 30
> > secs)
> >    testev.wait(30)
> >    if testev.isSet():
> 
> Unfortunately, COM's threading model bites here.  I suggest reading 
> "Python Programming on Win32" for a fairly good COM threading model and 
> how it relates to Python description.
> 
> Check out win32com\test\testMSOfficeEvents.py.  It does a simple:
> 
>          if msvcrt.kbhit():
>              msvcrt.getch()
>          pythoncom.PumpWaitingMessage()
> 
> loop.  Another (better) alternative would be 
> win32event.MsgWaitForMultipleObjects() - win32\Demos\timer_demo.py has 
> an example of this style of loop, and I am sure others would turn up.
> 
> Mark.



More information about the Python-list mailing list