Module initialization problem when using COM

Robin Becker robin at jessikat.demon.co.uk
Fri May 21 10:18:30 EDT 1999


In article <0E16861EE7BCD111BE9400805FE6841F0949A5B2 at c1s5x001.cor.srvfar
m.origin-it.com>, Robin Boerdijk <robin.boerdijk at nl.origin-it.com>
writes
>We have the following problem: The initialization function of the ILU
>extension module gets called more than once when used within a Python COM
>server and called from Visual Basic (and this makes ILU crash).
>
>To investigate the problem, we built the following small extension module,
>called "empty", that only has an initialization function that counts the
>number of times it gets called and logs this into a file:
>
>/* empty.cpp */
>
>#include <stdio.h>
>#include "Python.h"
>
>int counter = 0;
>
>static PyMethodDef empty_methods[] = {
>  {NULL, NULL}           /* sentinel */
>};
>
>void initempty()
>{
>  FILE* log;
>
>  log = fopen("c:\\empty.log", "a");
>  fprintf(log, "Counter: %d\n", counter);
>  fclose(log);
>
>  Py_InitModule("empty", empty_methods);
>
>  counter++;
>};
>
>We also have a simple Python COM server that imports this module and exposes
>a simple Hello method to be called from VB:
>
># testcomserver.py
>
>import empty
>
>class HelloWorld:
>
> _reg_clsid_ = "{112077C1-078D-11D3-8427-00600894A6FB}"
> _reg_desc_ = "Python Test COM Server"
> _reg_progid_ = "Python.TestServer"
> _reg_class_spec_ = "testcomserver.HelloWorld"
> _public_methods_ = ['Hello']
>
> def Hello(self, who):
>
>  return "Name: " + str(who)
>
>if __name__=='__main__':
>
> import win32com.server.register
> win32com.server.register.UseCommandLine(HelloWorld)
>
>Now, we call the Hello() method from the following Visual Basic program by
>clicking the button a few times.
>
>' testcomclient
>
>Private Sub Command1_Click()
>    Dim test As Object
>    Set test = CreateObject("python.testserver")
>    MsgBox test.Hello("Mark")
>End Sub
>
>The initempty() function from the Python extension module now gets called
>multiple times as we can see from the c:\empty.log file:
>
>Counter: 0
>Counter: 1
>Counter: 2
>
>Again, for ILU this causes the extension module to crash. Is this a VB,
>Python or COM problem ? Any ideas how to solve it ?
>
>Robin.
>
>
>
I'm not sure I see why you see this as a problem.

Each time you click on the button you instantiate an instance of
HelloWorld; this means the static Counter gets incremented as VB holds
the dlls and doesn't reload them. Presumably the ILU stuff has some
initialisation stuff which should only be done once across all instances
in the current process. To make it once over al instances requires some
kind of lock. 
-- 
Robin Becker




More information about the Python-list mailing list