Webware 0.7/Singleton pattern

Michael Bub mbub at meta-level.de
Tue Jul 16 03:45:41 EDT 2002


Hi folks,

I am currenty working on my diploma thesis. I chose to use Webware and
Python to build my WebApp.

During the development it turned out that some classes should become
singletons.
I implemented all my singleton classes with the following code:

class myClass:

    __instance=None

    def __init__(self):
       # init stuff
       pass

    def get_instance():
       if myClass.__instance is None:
           myClass.__instance=myClass()
       return myClass.__instance
    get_instance=staticmethod(get_instance)

This generally seems to work just fine when using it. However, in my
WebApp built with Webware 0.7 (using Python 2.2 on a FreeBSD4.4
system), the second or third time the get_instance method is called,
the one instance that was created by the singleton seems to be
forgotten about (or rather, it seems as if the file that holds the
class definition was imported and used for the very first time) and
another one is created. This, of course, sucks. I do not know where
the first one goes, its __del__ does not get called during that
process, but it is replaced by the second instance created by the
singleton. after that has happened, everything is back to normal and
the singleton classes behave as expected and do not create any new
objects.

does anyone know how to get "safe" singleton classes to get rid of
that problem?

-- 
Michael



More information about the Python-list mailing list