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

Peter Mott peter at monicol.co.uk
Fri Oct 10 05:35:00 EDT 2003


CoUninitialize frees resources that were used when the COM libraries were
intialized with CoInitialise. BTW as you are using the Dispatch interface which
is what scripting languages like VBscript use I don't see why you need to
initialise COM directly.

Peter

> -----Original Message-----
> From: python-win32-bounces at python.org
> [mailto:python-win32-bounces at python.org]On Behalf Of Moof
> Sent: 10 October 2003 09:50
> To: python-win32 at python.org
> Subject: [python-win32] Multithreaded COM, when do I need to
> CoUninitialise?
>
>
> 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'
>
>
>
> _______________________________________________
> Python-win32 mailing list
> Python-win32 at python.org
> http://mail.python.org/mailman/listinfo/python-win32
>





More information about the Python-win32 mailing list