[mod_python] COM errors on Win2K3

Graham Dumpleton grahamd at dscpl.com.au
Thu Jun 29 18:43:00 EDT 2006

douglas savitsky wrote ..
> I have an application that runs on mod_python/apache using the publisher
> handler.  It uses an Access db as the backend (yes, I know, but the
> amount of data is small so it should be fine) and connects via ADO.
> In a
> test environment on XP it runs fine, but when on a win2k3 server it will
> load one page, but all subsequent page loads throw a
> pythoncom.CoInitialize error until I restart the apache.
> The structure is that the python script imports a module that contains
> the ADO code.
> Presumably, this is a threading issue, but I am not sure what the best
> approach to fixing it is.  I tried putting
> pythoncom.CoInitialize/pythoncom.CoUninitialize calls in the __init__
> and __del__ methods of the modules, but this did not work. 

Modules do not have __init__ and __del__ methods, so that cannot work.

If you need to ensure that a specific bit of code is executed when Apache
starts up and that it is only executed once, you should stick it in a module
of its own and then use the PythonImport directive to tell mod_python
that that module should be loaded right at the start when mod_python
is initialised and before any requests are processed.

The code you put in this special module should be executed as a side
effect of the module import itself. In that way it acts like an __init__
method although there is not one. There is no safe way to have the
equivalent of __del__. Ie., no way you can have special code run when
Apache is being shutdown. Before you think that apache.register_cleanup()
or req.server.register_cleanup() might do it, it will not as that code has
been found to be unreliable and can cause Apache child process to
lock up on shutdown.

Also note that this special module imported using PythonImport should
not contain any handlers and should NOT be imported from handler code
using apache.import_module() otherwise it might be reloaded at run time
with initialisation then being performed more than once. Thus, use "import"
to import it instead. It is possible to have code within the module to protect
against reloading issues, but simply best to avoid it in the first place.


> Is this
> something that needs to be done at a lower level? Should I wrap every
> COM call or can I turn off threading somewhere?
> This is mod_python 3.2, Python 2.3.5, apache2.0.*, win2k3, ado 2.8, and
> the
> server has 2 processors in case that matters.
> The error I get is:
> Mod_python error: "PythonHandler mod_python.publisher"
> Traceback (most recent call last):
>   File "C:\Python23\Lib\site-packages\mod_python\apache.py", line 299,
> in
> HandlerDispatch
>     result = object(req)
>   File "C:\Python23\Lib\site-packages\mod_python\publisher.py", line 213,
> in
> handler
>     published = publish_object(req, object)
>   File "C:\Python23\Lib\site-packages\mod_python\publisher.py", line 410,
> in
> publish_object
>     return publish_object(req,util.apply_fs_data(object, req.form, req=req))
>   File "C:\Python23\Lib\site-packages\mod_python\util.py", line 439, in
> apply_fs_data
>     return object(**args)
>   File "E:\http_server\timeslips\index.py", line 84, in index
>     rtn.append(ts_widget.make_employee_picker())
>   File "C:\Python23\lib\site-packages\ecptimeslips\ts_widget.py", line
> 128,
> in make_employee_picker
>     rs = win32com.client.Dispatch("ADODB.Recordset")
>   File "C:\Python23\Lib\site-packages\win32com\client\__init__.py", line
> 95,
> in Dispatch
>     dispatch, userName =
> dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
>   File "C:\Python23\Lib\site-packages\win32com\client\dynamic.py", line
> 91,
> in _GetGoodDispatchAndUserName
>     return (_GetGoodDispatch(IDispatch, clsctx), userName)
>   File "C:\Python23\Lib\site-packages\win32com\client\dynamic.py", line
> 79,
> in _GetGoodDispatch
>     IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx,
> pythoncom.IID_IDispatch)
> com_error: (-2147221008, 'CoInitialize has not been called.', None, None)
> Even more strangely, with IE it will work for a bit and then die (but
> only for one user), with Firefox it never works, and with Mozilla it
> is in the middle -- maybe one page and then an error.
> ?
> Thanks,
> -d
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python

More information about the Mod_python mailing list