#-*-coding:utf-8-*-frommultiprocessingimportProcessfromsingletonimportsingletonimportosimportrandom@singletonclassDemo21(object):def__init__(self):self.name='Demo21'self.rand=random.randint(1,100)classDemo22(object):def__init__(self):self.name='Demo22'self.rand=random.randint(1,100)defcreate_class(cls):this=cls()print"ProcessID#{}ClassName#{}InstanceID#{}Info#{}rand#{}".\format(os.getpid(),this.name,this,id(this),this.rand)if__name__=='__main__':pool=[]forclsin[Demo21,Demo21,Demo22,Demo22]:p=Process(target=create_class,args=(cls,))pool.append(p)forpinpool:p.start()forpinpool:p.join()输出ProcessID#1304ClassName#Demo21InstanceID#<__main__.Demo21objectat0x1038ab0d0>Info#4354388176rand#29ProcessID#1305ClassName#Demo21InstanceID#<__main__.Demo21objectat0x1038ab0d0>Info#4354388176rand#36ProcessID#1306ClassName#Demo22InstanceID#<__main__.Demo22objectat0x1038ab0d0>Info#4354388176rand#16ProcessID#1307ClassName#Demo22InstanceID#<__main__.Demo22objectat0x1038ab0d0>Info#4354388176rand#10想知道为什么他们的地址是相同的?求详细解释
四季花海
相关分类