[Twisted-Python] win32com client in a twisted thread

I'm running a Windows service with Twisted (xmlrpc), and I've run into an odd case. In a recently implemented method, I create a COM object with win32com.client.Dispatch (using a COM DLL that's part of the overall application) and make a couple of calls on it, getting a return value. This works well, except that the COM object calls a web service, which can take a few seconds, so I wanted to defer it. When I isolate it into a function, and call that with deferToThread, the thread appears to die. I tried putting the Dispatch call in the mainline, then passing it to the function -- this kills Python (not too surprising really). I'm also using dbapi, so I tried it using runInteraction, with no better luck. For what it's worth, here's the code:
command = Dispatch('APTAuthorize.clsAuthorizeBatch') model = command.SetupFromConnParms(db['DSN'], db['UserName'], db['Password']) # The returned value is a tuple: the model followed by the params model = model[0] model.PaymentAdjustment = "P" model.BatchId = rsltDict['lPaymentBatchId'] model.ShowProgress = True model.AuthorizationType = "CCD" command.Execute(SecurityCode)
Putting logging statements in, I found that it died during the first call to "command". (With no error message or other indication.) Any thoughts or useful information appreciated, -- Don Dwiggins Advanced Publishing Technology

On Mon, 2010-05-03 at 17:32 -0700, Don Dwiggins wrote:
I'm running a Windows service with Twisted (xmlrpc), and I've run into an odd case.
In a recently implemented method, I create a COM object with win32com.client.Dispatch (using a COM DLL that's part of the overall application) and make a couple of calls on it, getting a return value. This works well, except that the COM object calls a web service, which can take a few seconds, so I wanted to defer it.
When I isolate it into a function, and call that with deferToThread, the thread appears to die. I tried putting the Dispatch call in the mainline, then passing it to the function -- this kills Python (not too surprising really).
As a test, you should try to make a minimal reproducing example without Twisted (e.g. just launching a new thread instead of using thread pool). My guess is that this is something to do with COM's threading rules (they're complex, apparently - the Python Win32 book has an appendix discussing the issues), or perhaps a bug in win32com. But that's just a guess, thus the need for a minimal example.

Don Dwiggins wrote:
I'm running a Windows service with Twisted (xmlrpc), and I've run into an odd case.
In a recently implemented method, I create a COM object with win32com.client.Dispatch (using a COM DLL that's part of the overall application) and make a couple of calls on it, getting a return value. This works well, except that the COM object calls a web service, which can take a few seconds, so I wanted to defer it.
When I isolate it into a function, and call that with deferToThread, the thread appears to die. I tried putting the Dispatch call in the mainline, then passing it to the function -- this kills Python (not too surprising really).
My memory of this stuff is *very* hazy, but IIRC, if you want to use win32com stuff from a non-main thread you need to call pythoncom.CoInitialize() in that thread. -Andrew.

Andrew, Itamar, thanks. I've taken the hint to CoInitialize the thread. That gets me a bit further, but there's still some strange stuff going on. I'm going to have to put this on the back burner for a while in favor of higher priority stuff. -- Don Dwiggins Advanced Publishing Technology

perhaps try to defer to a separate process? 2010/5/5, Don Dwiggins <ddwiggins@advpubtech.com>:
Andrew, Itamar, thanks. I've taken the hint to CoInitialize the thread. That gets me a bit further, but there's still some strange stuff going on. I'm going to have to put this on the back burner for a while in favor of higher priority stuff.
-- Don Dwiggins Advanced Publishing Technology
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
-- Nosūtīts no manas mobilās ierīces -- Konrads Smelkovs Applied IT sorcery.
participants (4)
-
Andrew Bennetts
-
Don Dwiggins
-
Itamar Turner-Trauring
-
Konrads Smelkovs