[python-win32] Multithreaded COM, when do I need to CoUninitialise?

Moof moof at metamoof.net
Fri Oct 10 04:49:37 EDT 2003


I'm currently writing a Webware app (http://webware.sf.net/), which uses 
multiple threads to serve web requests in the application server.

Due to the nature of my application, I have to save a number of COM 
objects in my sessions, which are held in memory, for about 15-30 
minutes, which is when the session times out and is garbage collected.

After playing around with the COMKit part of Webware and deciding it 
isn't as simple as it looks, I now find I'm doing a little something 
like this:

import sys
sys.coinit_flags = 0
import pythoncom
from Webkit.Page import Page
from win32com.client import Dispatch

class FooPage(Page):
	def procThatGetsCalledToSetUpData(self):
		#first reinit COM, in case this thread hasn't inited it
		pythoncom.CoInitializeEx(pythoncom.COINIT_MULTITHREADED)
		foo = Dipatch('comlib.object1')
		#do stuff with foo
		bar = Dispatch('comlib.object2')
		bar.doStuff(foo)
		self.session().setValue('foo',foo)
		self.session().setvalue('bar',bar)
		#And here I used to CoUninitialize()
		self.writePage()

The reason I *used* to CoUninitialize there was that at the time, I 
didn't need the com objects anymore, but now I find that I do, and need 
to store them in the session.

The thing I suppose I'm wondering is whether I need to worry about 
CoUninitialize - I have no control over which thread will be working 
with the COM objects later, and I can't even force the user to go 
through a set path in my webpage, the session might never reach 
completion. Basically, I don't know what CoUninitialize does exactly, 
and whether using it is just being "good neighbourly" or whether it's 
actually really necessary.

Since I'm asking, given that a thread may call CoInitializeEx() a number 
of times throughout its lifetime, is that going affect the COM Objects 
that are currently being stored in a session somewhere when I try to 
import them back into that thread?

Thanks in advance,
Moof
-- 
G.A.Radford - Moofing at you from Madrid, Spain
'Intelligence, Khalis, and Eupathy'





More information about the Python-win32 mailing list