[python-win32] Win32com events, multithreading, and re-entrancy
Moore, Paul
Paul.Moore@atosorigin.com
Wed, 25 Sep 2002 14:08:11 +0100
I'm getting in a muddle trying to understand what's going on here...
from win32com.client import DispatchWithEvents, constants
import sys
# sys.coinit_flags =3D 0
import pythoncom
finished =3D 0
class ADOEvents:
def OnWillConnect(self, str, user, pw, opt, sts, cn):
# Must have this event, as if it is not handled, ADO assumes the
# operation is cancelled, and raises an error (Operation =
cancelled
# by the user)
pass
def OnConnectComplete(self, error, status, connection):
# print "Connected to", connection.Properties("Data Source")
global finished
finished =3D 1
c =3D DispatchWithEvents("ADODB.Connection", ADOEvents)
str =3D "Provider=3Dmsdaora;Data Source=3DOEM.UKRUR053"
c.Open(str, "system", "manager", constants.adAsyncConnect)
while not finished:
pythoncom.PumpWaitingMessages()
print ".",
This is an ADO program, trying to catch the ADO connection events. The =
program works fine, *as long as* the print statement in =
OnConnectComplete is commented out. Any attempt to access the connection =
object from within the event handler causes Python to jump back into the =
main busy wait loop, pumping messages and printing dots.
I assume that this is somehow related to the internal multithreading and =
serialisation that goes on in COM, but I don't know enough about that to =
understand it. Trying to make the code free-threaded (assigning to =
sys.coinit_flags - BTW, there's a bug in the documentation for =
pythoncom.CoInitializeEx, which gives 2 variations on this name) didn't =
seem to help.
How do I do this? Is there any sample code which shows how to do this? =
Mark Hammond's book doesn't cover ADO events, and I've read the =
threading appendix and am still no wiser...
Actually, on a more general note, is there any sample win32com code =
around anywhere? The samples and tests included with the package are =
useful, but they only go so far. Also some of them are out of date (I =
was looking at the universal gateway stuff yesterday, and testuniv.py =
doesn't even load, as "import univgw" no longer works :-()
Thanks for any suggestions,
Paul.