title: Celery Task instanciation cache
body: <p>I'm trying to cache a large resource file among tasks using Celery 4.0.2.</p>
  
  <p>Looking it in the documentation , I have reach with the task caching part.</p>
  
  <p><a href="http://docs.celeryproject.org/en/latest/userguide/tasks.html#instantiation" rel="nofollow noreferrer">http://docs.celeryproject.org/en/latest/userguide/tasks.html#instantiation</a></p>
  
  <p><em>This can also be useful to cache resources, For example, a base Task class that caches a database connection:</em></p>
  
  <pre><code>from celery import Task
  
  class DatabaseTask(Task):
      _db = None
  
      @property
      def db(self):
          if self._db is None:
              self._db = Database.connect()
          return self._db
  </code></pre>
  
  <p>In my case I have done some changes to cache my big file resource, and the object its shared among the tasks,  but the memory used by big file resource are cached in the task forever.</p>
  
  <pre><code>from celery import Task
  
  class BigResourceTask(Task):
      _resource = None
  
      @property
      def resource(self):
          if self._resource is None:
              self._resource = load_big_resource()
          return self._resource 
  </code></pre>
  
  <p>How can I free that memory or make it expire after the execution of all the related tasks?</p>