[Zope-dev] Zope and Com : CoInitialize done on each thread by Zserver?

Brian Lloyd Brian@digicool.com
Mon, 30 Aug 1999 15:15:14 -0400


> Until this issue is resolved, where would you suggest that I place my 
> call to CoInitializeEX()?
> 
> It has to be called only once, when a thread is created.

(I'm CC'ing this to the list, since this trick may be
 useful to others...)

If it were me, I'd try _real_ hard to not have to hack up
the medusa code (which is where the actual threads get
created), and try to find a sneaky way to get the job done
from within your product. Here's one way you could do it
(not very pretty, but it should work without requiring
hacks to Zope code):

(in the .py file of your product)

_tmap_={}

def ensure_init():
    # waaa - make sure CoInitialize has been
    # called for this thread...
    id=thread.get_ident()
    if _tmap_.get(id, None) is not None:
        # this thread has been done already!
        return
    # otherwise we need to do it...
    win32com.CoInitializeEx(...)
    _tmap_[id]=1
    return


...now you should be able to stick an ensure_init() call into
your code before you start messing with COM, and the Right 
Thing ought to happen. It adds a little overhead, but in most
cases you could probably streamline this further depending on
how your code looks.

Hope this helps!
    





Brian Lloyd        brian@digicool.com
Software Engineer  540.371.6909              
Digital Creations  http://www.digicool.com