wait() method of threading.Event() class

Miranda Evans mirandacascade at yahoo.com
Wed May 8 18:51:32 EDT 2002


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():
      print "event happened within 30 seconds"
   else:
      print "event did not happen within 30 seconds"

In Interactive Window of PythonWin environment:
>>> import test
>>> test.driver()    
An MS Excel app opens...within a few seconds, I make the Excel app the
active window and double click in a cell.  I attempt to get back to
the PythonWin application...unable to get to that application until
about 30 seconds after the test.driver() function was invoked.  When
back at the PythonWin application, the message 'event did not happen
within 30 seconds' appears.

I ran the following test in Interactive Window 
>>> import win32com.client
>>> class xlEvents:
...    def OnSheetBeforeDoubleClick(self, sh, target, cancel):
...       print "double click happened"

>>> xl = win32com.client.DispatchWithEvents("Excel.Application",
xlEvents)
>>> xl.Visible=1
>>> xl.Workbooks.Add()

At this point I make the excel app the active window and I double
click in a cell.  Am able to get back to the PythonWin application
right away, and the message 'double click happened' is displayed. 
Based on this test, it looks like:
 - OnSheetBeforeDoubleClick is an event that fires
 - the DispatchWithEvents method is able to make the python script
aware of events that fire in the Excel app

So, it kinda looks like I didn't implement the threading.Event class
correctly.  How should I have implemented this to make the application
wait until either the double click event fires or until 30 seconds
elapses; whichever comes first?



More information about the Python-list mailing list