Python COM server running as a singleton
Marc ENGEL
marc.engel at recif.com
Fri Jan 18 05:02:46 EST 2002
Thanks for the help.
Actually I already tried this solution, but I could not get it working
properly. The reason is that the function RegisterServer in file
register.py does not remove the key from the registery for
INPROC_SERVER. And my server was first registered as InprocServer. So
I had to remove the key manually from the registery to get it running
as CLSCTX_LOCAL_SERVER
I would suggest to add an else statement for the if at line 176:
if not clsctx or clsctx & pythoncom.CLSCTX_INPROC_SERVER:
........
else:
_remove_key(keyNameRoot + "\\InprocServer32")
Of course, it could be done also for CLSCTX_LOCAL_SERVER if statement:
if not clsctx or clsctx & pythoncom.CLSCTX_LOCAL_SERVER:
Does this make sense?
Just to end with this singleton thing, here is the code I used:
# Global variable added into module Dictionary.py
SingletonInstance = None
# ........
# Code modified inside DictionaryPolicy class
_reg_clsctx_ = pythoncom.CLSCTX_LOCAL_SERVER
def _CreateInstance_(self, clsid, reqIID):
global SingletonInstance
# Do this to get it a singleton
if SingletonInstance == None:
# If the instance does not exist, we need to create it
self._wrap_({ })
SingletonInstance = self
else :
# Wrap the existing object
self._wrap_(SingletonInstance._obj_)
object = pythoncom.WrapObject(self, reqIID)
return object
# End of code
Marc
Gordon McMillan <gmcm at hypernet.com> wrote in message news:<Xns9199A09D7AEDgmcmhypernetcom at 199.171.54.214>...
> Marc ENGEL wrote:
>
> > I want to be able to get a COM server written in Python running as a
> > singleton. I also want to Dispatch it from VB and from Python and to
> > get the same instance.
>
> [snip]
>
> > Here is a solution I found using GetActiveObject.
>
> [snip]
>
> You've probably gone to too much work :-(. At least, here's
> what I've found (which may well have holes in it):
>
> If you use CLSCTX_LOCAL_SERVER, then COM will only start
> one copy of your script. Each process using it will
> (normally) get a fresh *instance* of your COM class, but
> that fairly easy to get around. You could probably even
> use Alex's borg pattern.
>
> To be non-tricky about it, write (and register) a COM class
> that has one exposed method. That method returns a module
> global instance of the class you're really interested in
> (creating it if need be). You'll need to Wrap it, of course.
>
> -- Gordon
> http://www.mcmillan-inc.com/
More information about the Python-list
mailing list