Twisted application中TCPClient的使用

一般,在Twisted中使用Application的方式启动程序,是这样做:

 

折叠复制代码
    1. pop_service = internet.TCPServer(…,…)
  • popService = service.MultiService()
  • pop_service.setServiceParent(popService)
  • application= service.Application('popqueueservice')
  • popService.setServiceParent(application)

 

 

通过setServiceParent来把某一个服务运行在application中

如果程序中我们需要生成很多TCPClient对象,去做其它的很多事情,那么我们不能这么做:

 

折叠复制代码
    1. op_service = internet.TCPClient(popserver,popport,f)
  • pop_service.setServiceParent(popService)

 

 




这样做是有很大问题的,因为每一个client的请求都被加入到了application中去运行

即使client执行完毕,也不会去释放自己对应的Factory实例和Protocol实例

长时间会造成内存中有很多这样的实例对象,内存不断增加,最后内存溢出…


这样的内存泄露,不太好定位,我之前检查了程序的各个地方,把能释放的资源都给释放了

唯独没有考虑到这个地方,最后使用了meliae工具,才定位到这个地方

然后这么修改的:

 

折叠复制代码
    1. pop_service.startService()
  • f.deferred.addCallback(handleEnd,id,pop_service)
  • 。。。。。。
  • #在每个client执行完毕后的deffer中调用了
  • s.stopService()

 

 

这样修改后,观察了几天,内存一直都比较稳定

posted on 2012-05-24 13:35  很多不懂呀。。  阅读(1157)  评论(0编辑  收藏  举报

导航