Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
773 views
in Technique[技术] by (71.8m points)

python - win32com.client.Dispatch + Cherrypy = CoInitialize has not been called

The following code works well, but it fails if executed from a CherryPy app method with the error message CoInitialize has not been called:

import win32com.client
xl = win32com.client.Dispatch("Excel.Application")
xl.quit()

This post suggests a solution that works for me:

import pythoncom
pythoncom.CoInitialize()

The reason I'm asking about a problem for which I already have a solution, is that (1) I would like to know what I'm doing (rather than doing it only because I've seen it working once) and (2) I don't want to risk to miss something important (and reading this post makes me think that I am missing something.)

I couldn't find any documentation for pythoncom.CoInitialize(), and the source of pythoncom is the following three lines that don't help me (nor Eclipse+pydev which says that the method does not exist):

# Magic utility that "redirects" to pythoncomxx.dll
import pywintypes
pywintypes.__import_pywin32_system_module__("pythoncom", globals())
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I can't remember exactly as I didn't work with COM last years but I guess that you have to initialize COM in every thread you work with it (again I'm not sure about every COM compartment). As CherryPy is threaded servers your requests are handled by different threads, not one you bootstrap with. So I suggest you to try the following in your bootstrap routine:

import pythoncom


def onThreadStart(threadIndex):
  pythoncom.CoInitialize()

cherrypy.engine.subscribe('start_thread', onThreadStart)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...