简体   繁体   中英

Limit number of class instances with python

Μy Mainclass creates a simple QmainWindows like this:

class mcManageUiC(QtGui.QMainWindow):
    def __init__(self):
        super(mcManageUiC, self).__init__()

        self.initUI()

    def initUI(self):
        self.show()

And at the end of my file I launch it like this:

def main():
    app = QtGui.QApplication(sys.argv)
    renderManagerVar = mcManageUiC()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

My problem is that each time i source it, it launches a new window. I would like to know if there is a way to detect existence of previous class instance in my script (so that I close the old one or avoid launching a new one), or any other solutions?

Also, when compiling my code with py2exe, same problem with my .exe file on Windows; it launchs a new window every time. Could i add something in the setup.py for Windows to not act like this?

Is it possible, if yes then how?

Note: I'm using Windows 7 64bit compiling with eclipse.

There are a couple ways to do this, you can use a Class attribute to store all the instances -- If you do it this way, you may want to store them as weak references via the weakref module to prevent issues with garbage collecting:

class MyClass(object):
    _instances=[]
    def __init__(self):
        if(len(self._instances) > 2):
            self._instances.pop(0).kill() #kill the oldest instance
        self._instances.append(self)

    def kill(self):
        pass #Do something to kill the instance

This is a little ugly though. You might also want to consider using some sort of Factory which (conditionally) creates a new instance. This method is a little more general.

import weakref
class Factory(object):
     def __init__(self,cls,nallowed):
         self.product_class=cls  #What class this Factory produces
         self.nallowed=nallowed  #Number of instances allowed
         self.products=[]

     def __call__(self,*args,**kwargs):
         self.products=[x for x in self.products if x() is not None] #filter out dead objects
         if(len(self.products) <= self.nallowed):
             newproduct=self.product_class(*args,**kwargs)
             self.products.append(weakref.ref(newproduct))
             return newproduct
         else:
             return None

#This factory will create up to 2 instances of MyClass
#and refuse to create more until at least one of those 
#instances have died.
factory=Factory(MyClass,2)   
i1=factory("foo","bar")      #instance of MyClass
i2=factory("bar","baz")      #instance of MyClass
i3=factory("baz","chicken")  #None

You can limit the number of instances you want to create in your code just by adding a counter:

class A(object):
 ins = 0 # This is a static counter
 def __init__(self):
  if A.ins >= 1: # Check if the number of instances present are more than one. 
   del self
   print "Failed to create another instance" #if > 1, del self and return.
   return
  A.ins += 1
  print "Success",str(self)

Try running via:

lst = []
for i in range(1,101):
 a=A()
 lst.append(a)

you could monopolize a socket

import socket
try:    
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except:
    "Network Error!"

s.settimeout(30)
try:
    s.connect(('localhost' , 123))
except:
    "could not open...already in use socket(program already running?)"

no idea if this is a good method but I have used it in the past and it solves this problem

this was designed to prevent launching a program when it was already running not from launching a new window from within a single script that is spawning several windows...

Use a class variable:

class mcManageUiC(QtGui.QMainWindow):
    singleton = None
    def __init__(self):
        if not mcManageUiC.singleton: #if no instance yet
            super(mcManageUiC, self).__init__()

            self.initUI()
            ...
            mcManageUiC.singleton = self
        else: 
            ...


    def initUI(self):
        self.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM