2012-05-03 89 views
7

我一直在尝试使用tornado-redis(这基本上是brükva的一个分支,略微修改为与tornado.gen接口而不是adisp一起使用),以便通过使用redis' pubsub来传递事件。如何使用pympler跟踪/修复龙卷风 - redis中的内存泄漏?

所以我写下了一个小脚本来测试一下this example的灵感。

import os 

from tornado import ioloop, gen 
import tornadoredis 


print os.getpid() 

def on_message(msg): 
    print msg 

@gen.engine 
def listen(): 
    c = tornadoredis.Client() 
    c.connect() 
    yield gen.Task(c.subscribe, 'channel') 
    c.listen(on_message) 

listen() 

ioloop.IOLoop.instance().start() 

不幸的是,我PUBLISH版通过redis-cli内存使用量持续上升。

为了剖析内存使用情况,我第一次尝试使用guppy-pe,但它在python 2.7下不起作用(是的,甚至尝试过trunk),所以我回到pympler

import os 

from pympler import tracker 
from tornado import ioloop, gen 
import tornadoredis 


print os.getpid() 

class MessageHandler(object): 

    def __init__(self): 
     self.memory_tracker = tracker.SummaryTracker() 

    def on_message(self, msg): 
     self.memory_tracker.print_diff() 

@gen.engine 
def listen(): 
    c = tornadoredis.Client() 
    c.connect() 
    yield gen.Task(c.subscribe, 'channel') 
    c.listen(MessageHandler().on_message) 

listen() 

ioloop.IOLoop.instance().start() 

现在每次我PUBLISH版,我可以看到一些对象从未发布:

          types | # objects | total size 
===================================================== | =========== | ============ 
               dict |   32 |  14.75 KB 
               tuple |   41 |  3.66 KB 
                set |   8 |  1.81 KB 
             instancemethod |   16 |  1.25 KB 
               cell |   22 |  1.20 KB 
          function (handle_exception) |   8 | 960  B 
            function (inner) |   7 | 840  B 
              generator |   8 | 640  B 
          <class 'tornado.gen.Task |   8 | 512  B 
          <class 'tornado.gen.Runner |   8 | 512  B 
    <class 'tornado.stack_context.ExceptionStackContext |   8 | 512  B 
               list |   3 | 504  B 
                str |   7 | 353  B 
                int |   7 | 168  B 
          builtin_function_or_method |   2 | 144  B 
               types | # objects | total size 
===================================================== | =========== | ============ 
               dict |   32 |  14.75 KB 
               tuple |   42 |  4.23 KB 
                set |   8 |  1.81 KB 
               cell |   24 |  1.31 KB 
             instancemethod |   16 |  1.25 KB 
          function (handle_exception) |   8 | 960  B 
            function (inner) |   8 | 960  B 
              generator |   8 | 640  B 
          <class 'tornado.gen.Task |   8 | 512  B 
          <class 'tornado.gen.Runner |   8 | 512  B 
    <class 'tornado.stack_context.ExceptionStackContext |   8 | 512  B 
               object |   8 | 128  B 
                str |   2 | 116  B 
                int |   1 |  24  B 
               types | # objects | total size 
===================================================== | =========== | ============ 
               dict |   32 |  14.75 KB 
               tuple |   42 |  4.73 KB 
                set |   8 |  1.81 KB 
               cell |   24 |  1.31 KB 
             instancemethod |   16 |  1.25 KB 
          function (handle_exception) |   8 | 960  B 
            function (inner) |   8 | 960  B 
              generator |   8 | 640  B 
          <class 'tornado.gen.Task |   8 | 512  B 
          <class 'tornado.gen.Runner |   8 | 512  B 
    <class 'tornado.stack_context.ExceptionStackContext |   8 | 512  B 
               list |   0 | 240  B 
               object |   8 | 128  B 
                int |   -1 | -24  B 
                str |   0 | -34  B 

现在,我知道有一个真正的内存泄漏,我该如何跟踪其中创建这些对象?我想我应该开始here

回答