__del__ pattern?

Chris Curvey ccurvey at gmail.com
Mon Aug 15 15:31:10 CEST 2005

I need to ensure that there is only one instance of my python class on
my machine at a given time.  (Not within an interpreter -- that would
just be a singleton -- but on the machine.)  These instances are
created and destroyed, but there can be only one at a time.

So when my class is instantiated, I create a little lock file, and I
have a __del__ method that deletes the lock file.  Unfortunately, there
seem to be some circumstances where my lock file is not getting
deleted.  Then all the jobs that need that "special" class start
queueing up requests, and I get phone calls in the middle of the night.

Is there a better pattern to follow than using a __del__ method?  I
just need to be absolutely, positively sure of two things:

1) There is only one instance of my special class on the machine at a
2) If my special class is destroyed for any reason, I need to be able
to create another instance of the class.

More information about the Python-list mailing list