• <tfoot id='vCJ6X'></tfoot>
      <bdo id='vCJ6X'></bdo><ul id='vCJ6X'></ul>

      <i id='vCJ6X'><tr id='vCJ6X'><dt id='vCJ6X'><q id='vCJ6X'><span id='vCJ6X'><b id='vCJ6X'><form id='vCJ6X'><ins id='vCJ6X'></ins><ul id='vCJ6X'></ul><sub id='vCJ6X'></sub></form><legend id='vCJ6X'></legend><bdo id='vCJ6X'><pre id='vCJ6X'><center id='vCJ6X'></center></pre></bdo></b><th id='vCJ6X'></th></span></q></dt></tr></i><div id='vCJ6X'><tfoot id='vCJ6X'></tfoot><dl id='vCJ6X'><fieldset id='vCJ6X'></fieldset></dl></div>

      <small id='vCJ6X'></small><noframes id='vCJ6X'>

    1. <legend id='vCJ6X'><style id='vCJ6X'><dir id='vCJ6X'><q id='vCJ6X'></q></dir></style></legend>

      1. 带有实例方法的Python函数工具lru_cache:Release对象

        Python functools lru_cache with instance methods: release object(带有实例方法的Python函数工具lru_cache:Release对象)

              1. <tfoot id='UcdXn'></tfoot>
                  <tbody id='UcdXn'></tbody>

                <small id='UcdXn'></small><noframes id='UcdXn'>

                <i id='UcdXn'><tr id='UcdXn'><dt id='UcdXn'><q id='UcdXn'><span id='UcdXn'><b id='UcdXn'><form id='UcdXn'><ins id='UcdXn'></ins><ul id='UcdXn'></ul><sub id='UcdXn'></sub></form><legend id='UcdXn'></legend><bdo id='UcdXn'><pre id='UcdXn'><center id='UcdXn'></center></pre></bdo></b><th id='UcdXn'></th></span></q></dt></tr></i><div id='UcdXn'><tfoot id='UcdXn'></tfoot><dl id='UcdXn'><fieldset id='UcdXn'></fieldset></dl></div>

                <legend id='UcdXn'><style id='UcdXn'><dir id='UcdXn'><q id='UcdXn'></q></dir></style></legend>
                  <bdo id='UcdXn'></bdo><ul id='UcdXn'></ul>
                  本文介绍了带有实例方法的Python函数工具lru_cache:Release对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如何在不泄漏内存的情况下在类内使用functools.lru_cache

                  在下面的最小示例中,foo实例将不会被释放,尽管它超出作用域并且没有引用对象(除lru_cache以外)。

                  from functools import lru_cache
                  class BigClass:
                      pass
                  class Foo:
                      def __init__(self):
                          self.big = BigClass()
                      @lru_cache(maxsize=16)
                      def cached_method(self, x):
                          return x + 5
                  
                  def fun():
                      foo = Foo()
                      print(foo.cached_method(10))
                      print(foo.cached_method(10)) # use cache
                      return 'something'
                  
                  fun()
                  

                  但是foo,因此foo.big(aBigClass)仍然有效

                  import gc; gc.collect()  # collect garbage
                  len([obj for obj in gc.get_objects() if isinstance(obj, Foo)]) # is 1
                  

                  这意味着Foo/BigClass实例仍驻留在内存中。即使删除Foo(delFoo)也不会释放它们。

                  为什么lru_cache要保留实例?缓存不是使用某些哈希而不是实际对象吗?

                  建议在类内使用lru_cache的方式是什么?

                  我知道两种解决方法: Use per instance caches或make the cache ignore object(但可能导致错误结果)

                  推荐答案

                  这不是最干净的解决方案,但对程序员完全透明:

                  import functools
                  import weakref
                  
                  def memoized_method(*lru_args, **lru_kwargs):
                      def decorator(func):
                          @functools.wraps(func)
                          def wrapped_func(self, *args, **kwargs):
                              # We're storing the wrapped method inside the instance. If we had
                              # a strong reference to self the instance would never die.
                              self_weak = weakref.ref(self)
                              @functools.wraps(func)
                              @functools.lru_cache(*lru_args, **lru_kwargs)
                              def cached_method(*args, **kwargs):
                                  return func(self_weak(), *args, **kwargs)
                              setattr(self, func.__name__, cached_method)
                              return cached_method(*args, **kwargs)
                          return wrapped_func
                      return decorator
                  
                  它采用与lru_cache完全相同的参数,并且工作方式完全相同。但是,它从不将self传递给lru_cache,而是使用每个实例的lru_cache

                  这篇关于带有实例方法的Python函数工具lru_cache:Release对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)
                  <tfoot id='0SqY1'></tfoot>
                  <legend id='0SqY1'><style id='0SqY1'><dir id='0SqY1'><q id='0SqY1'></q></dir></style></legend>

                  <i id='0SqY1'><tr id='0SqY1'><dt id='0SqY1'><q id='0SqY1'><span id='0SqY1'><b id='0SqY1'><form id='0SqY1'><ins id='0SqY1'></ins><ul id='0SqY1'></ul><sub id='0SqY1'></sub></form><legend id='0SqY1'></legend><bdo id='0SqY1'><pre id='0SqY1'><center id='0SqY1'></center></pre></bdo></b><th id='0SqY1'></th></span></q></dt></tr></i><div id='0SqY1'><tfoot id='0SqY1'></tfoot><dl id='0SqY1'><fieldset id='0SqY1'></fieldset></dl></div>
                      <tbody id='0SqY1'></tbody>

                  • <small id='0SqY1'></small><noframes id='0SqY1'>

                    • <bdo id='0SqY1'></bdo><ul id='0SqY1'></ul>