在做多线程的一个例子时,里面用到了win32,结果运行时出错:
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Python25\lib\threading.py", line 486, in __bootstrap_inner
self.run()
File "C:\Python25\lib\threading.py", line 446, in run
self.__target(*self.__args, **self.__kwargs)
File "C:\2.py", line 124, in startDeal
dealWithEmail(host,user,pwd)
File "C:\2.py", line 174, in dealWithEmail
typeSetting(name) #鎺掔増
File "C:\2.py", line 104, in typeSetting
xls = easyExcel(name)
File "C:\2.py", line 25, in __init__
self.xlApp = win32com.client.Dispatch('Excel.Application')
File "C:\Python25\Lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch
dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
File "C:\Python25\Lib\site-packages\win32com\client\dynamic.py", line 104, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
File "C:\Python25\Lib\site-packages\win32com\client\dynamic.py", line 84, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
com_error: (-2147221008, '\xc9\xd0\xce\xb4\xb5\xf7\xd3\xc3 CoInitialize\xa1\xa3', None, None)
查了一下,每个进程执行时需要加上一句:pythoncom.CoInitialize()
解决。