BaseHTTPServer, ThreadingMixIn and COM.

Arpad Kiss sekter at matavnet.hu
Tue Mar 14 14:51:47 CET 2000


Hi,
The answer is in the error message you got: pywintypes.com_error:
(-2147221008, 'CoInitialize has not been called.', None, None)
You have to call CoInitialize in every threads before you use any other COM
functionality.
Arpad


----- Original Message -----
From: Paul Robinson <paul.robinson at quantisci.co.uk>
To: Python Group <python-list at python.org>
Sent: Tuesday, March 14, 2000 2:36 PM
Subject: BaseHTTPServer, ThreadingMixIn and COM.


>
> Is there some inherant problem with using code such as...
>
> ExcelApp=win32com.client.Dispatch("Excel.Application")
> ExcelApp.visible = 0
> workbook = ExcelApp.Workbooks.Add()
> worksheet=workbook.Worksheets.Add()
> cell = worksheet.Cells(1, 1)
> val = unquote('=(2+3)*7')
> cell.Value = val
> val = cell.Value
> ExcelApp.DisplayAlerts=0
> workbook.Close()
> ExcelApp.DisplayAlerts=1
> ExcelApp.Quit()
>
> ...inside the request handler of a server that looks like:
> from BaseHTTPServer import HTTPServer
> from SocketServer import ThreadingMixIn
> class AServer(ThreadingMixIn, HTTPServer):
> pass
> The request handler is subclassed from BaseHTTPRequestHandler and I'm
> simply overriding the do_GET method.
>
> When the server does not include the ThreadingMixIn then all is well,
> but it seems that I'm running into problems opening these Excel objects
> in threads.
> I presume that it's my code being thread-un-safe rather than the
> win32com stuff in general.
> What precautions should I be taking and/or how can I avoid this problem?
>
> The actual error I get is as follows:
> [Note that it's after one succesful request to the server]
>
> D:\Temp\pySERV>python server.py
> localhost - - [14/Mar/2000 13:33:28] "GET /%3d%282%2b7%29%2a12 HTTP/1.1"
> 200 -
> Unhandled exception in thread:
> Traceback (innermost last):
>   File "C:\PROGRA~1\PYTHON\LIB\SocketServer.py", line 243, in
> finish_request
>     self.RequestHandlerClass(request, client_address, self)
>   File "C:\PROGRA~1\PYTHON\LIB\SocketServer.py", line 366, in __init__
>     self.handle()
>   File "C:\PROGRA~1\PYTHON\LIB\BaseHTTPServer.py", line 258, in handle
>     method()
>   File "simpleExcel.py", line 10, in behaviourGET
>     ExcelApp=win32com.client.Dispatch("Excel.Application")
>   File "C:\Program Files\Python\win32com\client\__init__.py", line 13,
> in Dispatch
>     dispatch, userName =
> dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
>   File "C:\Program Files\Python\win32com\client\dynamic.py", line 67, in
> _GetGoodDispatchAndUserName
>     return (_GetGoodDispatch(IDispatch, clsctx), userName)
>   File "C:\Program Files\Python\win32com\client\dynamic.py", line 58, in
> _GetGoodDispatch
>     IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx,
> pythoncom.IID_IDispatch)
> pywintypes.com_error: (-2147221008, 'CoInitialize has not been called.',
> None, None)
>
> [WinNT 4, SP4, Python 1.5.2, PythonWin Build 127]
> Any help would be appreciated.
>
> Paul.
>
> -----------------------------------
> Business Collaborator Team
> Enviros Software Solutions
> http://www.businesscollaborator.com
> -----------------------------------
>
> --
> http://www.python.org/mailman/listinfo/python-list
>





More information about the Python-list mailing list