[python-win32] Dispatch error 'CoInitialize has not been called'

Mark Hammond mhammond at skippinet.com.au
Tue Jan 2 04:17:33 CET 2007


Another random comment re CoInitialize in servers...

Depending on your requirements, you may be better off with:

try:
  pythoncom.CoInitializeEx(pythoncom.COINIT_MULTITHREADED)
except pythoncom.com_error:
  # already initialized.
  pass

(and as per Roger's comments, adding a flag to handle CoUninitialize).  That
way, you are more likely to get a COM object that can be shared across
multiple threads (eg, grabbing a shared ActiveDirectory connection), and
also handle the possibility someone else has beaten you to the Initialize
using different flags (a distinct possibility in a "plugin" style server
environment.)  Also, many server environments, such as Zope and I guess
Apache, use some kind of thread-pool mechanism.  Threads are often started
early, and only terminated as the process terminates (or whenever the server
feels the need to recycle threads for whatever reason).  For this reason, I
admit that not all code I write ensures CoUninitialize() is always called,
even though I should :)

Mark
-------------- next part --------------
A non-text attachment was scrubbed...
Name: winmail.dat
Type: application/ms-tnef
Size: 2180 bytes
Desc: not available
Url : http://mail.python.org/pipermail/python-win32/attachments/20070102/e3270cd8/attachment.bin 


More information about the Python-win32 mailing list