最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

python内存释放原则

来源:动视网 责编:小采 时间:2020-11-27 14:27:22
文档

python内存释放原则

python内存释放原则:def getInit(class_name): """动态加载模块""" resultmodule = __import__(class_name, globals(), locals(), [class_name]) resultclass = getattr(resultmodule,
推荐度:
导读python内存释放原则:def getInit(class_name): """动态加载模块""" resultmodule = __import__(class_name, globals(), locals(), [class_name]) resultclass = getattr(resultmodule,


def getInit(class_name):
 """动态加载模块"""
 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 "
"
 print "start"
 c.run()
 print "end"
 print "
"
 
if __name__ == '__main__':
 param = {}
 th = threading.Thread(target = treading, args = ())
 th.start()
 
exit()
b is inited
start
a is inited
a is run
a is run
b is run
a is deleted
end
start
a is inited
a is run
a is run
b is run
a is deleted
end
b is deleted

每个循环内都把a重新加载并且释放内存

def getInit(class_name):
 resultmodule = __import__(class_name, globals(), locals(), [class_name])
 resultclass = getattr(resultmodule, class_name)
 return resultclass()
import threading, time
import 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 "
"
 print "start"
 c.run()
 print "end"
 print "
"
 
if __name__ == '__main__':
 param = {}
 th = threading.Thread(target = treading, args = ())
 th.start()
 
exit()
a is inited
b is inited
start
a is run
a is run
b is run
end
start
a is run
a is run
b is run
end
b is deleted
a is deleted

最后结束a才释放


内存释放是根据指向的

文档

python内存释放原则

python内存释放原则:def getInit(class_name): """动态加载模块""" resultmodule = __import__(class_name, globals(), locals(), [class_name]) resultclass = getattr(resultmodule,
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top