[Twisted-Python] adbapi,adodbapi and pythoncom.CoInitialize()

Andrew Bennetts andrew-twisted at puzzling.org
Mon Jun 14 18:11:15 MDT 2004


On Mon, Jun 14, 2004 at 05:47:45PM -0600, Craig H. Anderson wrote:
> 
> 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.





More information about the Twisted-Python mailing list