DefgetInit (class_name): & amp; quot; dynamic loading Module & amp; quot; resultmodule _ import _ (class_name, globals (), locals (), [class_name]) resultclassgetattr (resultmodule, class_n...
Def getInit (class_name): "dynamic loading module" resultmodule = _ import _ (class_name, globals (), locals (), [class_name]) resultclass = getattr (resultmodule, class_name) return resultclass () import threading, time class B: def _ init _ (self, * args, ** kwargs): self. name = "B" self. class_name = "a" print "% s is inited" % (self. name) def _ del _ (self): print "% s is deleted" % (self. name) def other_run (self, obj = None): if obj: obj. run () def run (self, * args, ** kwargs): obj = getInit (self. class_name) obj. run () self. other_run (obj) print "% s is run" % (self. name) def treading (): n = 0 c = B () while n <2: n + = 1 print "\ n" print "start" c. run () print "end" print "\ n" if _ name _ = '_ main _': param = {} th = threading. thread (target = treading, args = () th. start () exit ()
b is initedstarta is initeda is runa is runb is runa is deletedendstarta is initeda is runa is runb is runa is deletedendb is deleted
Reload a and release the memory in each loop.
def getInit(class_name): resultmodule = __import__(class_name, globals(), locals(), [class_name]) resultclass = getattr(resultmodule, class_name) return resultclass()import threading, timeimport a class b: def __init__(self, *args, **kwargs): self.name = "b" self.class_name = "a" self.obj = getInit(self.class_name) print "%s is inited" % (self.name) def __del__(self): print "%s is deleted" % (self.name) def other_run(self): if self.obj: self.obj.run() def run(self, *args, **kwargs): self.obj.run() self.other_run() print "%s is run" % (self.name) def treading(): n = 0 c = b() while n<2: n += 1 print "\n" print "start" c.run() print "end" print "\n" if __name__ == '__main__': param = {} th = threading.Thread(target = treading, args = ()) th.start() exit()
a is initedb is initedstarta is runa is runb is runendstarta is runa is runb is runendb is deleteda is deleted
Release only after end
Memory is released based on