wait() method of threading.Event() class

Mark Hammond mhammond at skippinet.com.au
Wed May 8 19:10:46 EDT 2002


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