Discussion:
[Twisted-Python] adbapi,adodbapi and pythoncom.CoInitialize()
Craig H. Anderson
2004-06-14 23:47:45 UTC
Permalink
I would like to use adbapi with the com based adodbapi.
I found that the connect call hangs unless I add a call
to pythoncom.CoInitialize().

I guess I'm now the tester for using adbapi and adodbapi on
windows.

I found a reference to the problem at:
http://devnulled.com/archives/python.php



For a crude test I added pythoncom.CoInitialize()
when a new connection is made in
twisted.enterprise.adbapi.ConnectionPool.connect()
The connection and sql query then run successfully.

#
import pythoncom
import sys

sys.coinit_flags = 0
pythoncom.CoInitialize()
[ COM code here... ]
pythoncom.CoUninitialize()
Andrew Bennetts
2004-06-15 00:11:15 UTC
Permalink
Post by Craig H. Anderson
I would like to use adbapi with the com based adodbapi.
I found that the connect call hangs unless I add a call
to pythoncom.CoInitialize().
That doesn't surprise me, seeing as adbapi runs stuff in threads to make it
asynchronous, and COM requires each thread that uses COM stuff to be
CoInitialize()d.

I'm not sure how to cleanly arrange for that to happen, though. Perhaps
something like:

----
import pythoncom
from twisted.python import threadpool
from twisted.enterprise import adbapi

class COMThreadPool(threadpool.ThreadPool):
def _worker(self, o):
pythoncom.CoInitialize()
threadpool.ThreadPool._worker(o)
pythoncom.CoUninitialize()

class COMConnectionPool(adbapi.ConnectionPool):
def __init__(self, dbapiName, *args, **kwargs):
adbapi.ConnectionPool.__init__(self, dbapiName, *args, **kwargs)
self.threadpool = COMThreadPool(self.min, self.max)
----

Then use COMConnectionPool rather than adbapi.ConnectionPool.

-Andrew.
Dave Peticolas
2004-06-15 01:00:27 UTC
Permalink
I think that is a good solution. I've used something similar on occasion.
Note that if you create the connection pool after the reactor has started,
you may need to stop the old threadpool.

dave

---- Original Message ----
From: Andrew Bennetts
Date: Mon 6/14/04 17:45
To: Twisted discussion stuff
Subject: Re: [Twisted-Python] adbapi,adodbapi and pythoncom.CoInitialize()
Post by Craig H. Anderson
I would like to use adbapi with the com based adodbapi.
I found that the connect call hangs unless I add a call
to pythoncom.CoInitialize().
That doesn't surprise me, seeing as adbapi runs stuff in threads to make it
asynchronous, and COM requires each thread that uses COM stuff to be
CoInitialize()d.

I'm not sure how to cleanly arrange for that to happen, though. Perhaps
something like:

----
import pythoncom
from twisted.python import threadpool
from twisted.enterprise import adbapi

class COMThreadPool(threadpool.ThreadPool):
def _worker(self, o):
pythoncom.CoInitialize()
threadpool.ThreadPool._worker(o)
pythoncom.CoUninitialize()

class COMConnectionPool(adbapi.ConnectionPool):
def __init__(self, dbapiName, *args, **kwargs):
adbapi.ConnectionPool.__init__(self, dbapiName, *args, **kwargs)
self.threadpool = COMThreadPool(self.min, self.max)
----

Then use COMConnectionPool rather than adbapi.ConnectionPool.

-Andrew.

Loading...