johnny-cache icon indicating copy to clipboard operation
johnny-cache copied to clipboard

utils.celery_enable_all still needed with cache.enable() ?

Open tsailiming opened this issue 11 years ago • 2 comments

I have the following in my proj/__init__.py:

from johnny.cache import enable
enable()

I find that if I spawn an async celery task in ./manage.py shell, the cache will be correct even without the celery patch:

from johnny.utils import celery_enable_all
celery_enable_all()

Looking at the code, why is unpatch needed? Is it necessary at all?

def prerun_handler(*args, **kwargs):
    """Celery pre-run handler.  Enables johnny-cache."""
    patch()

def postrun_handler(*args, **kwargs):
    """Celery postrun handler.  Unpatches and clears the localstore."""
    unpatch()
    local.clear()

def celery_enable_all():
    """Enable johnny-cache in all celery tasks, clearing the local-store
    after each task."""
    from celery.signals import task_prerun, task_postrun, task_failure
    task_prerun.connect(prerun_handler)
    task_postrun.connect(postrun_handler)
    # Also have to cleanup on failure.
    task_failure.connect(postrun_handler)

tsailiming avatar Oct 13 '13 15:10 tsailiming

Hello, If you enable the cache (and never disable it), then really the enable at the top should be enough. That's not always possible though if you need to run manual sql/etc in the tasks, so that's why we include the enable all as a convenience.

Jeremy

jself avatar Oct 13 '13 19:10 jself

Thanks. I noticed that in my normal unit tests (not celery tasks), I had to clear the localstore in setUp in addition to cache.clear() , otherwise the QuerySet can return results even though the .count() is 0.

tsailiming avatar Oct 14 '13 00:10 tsailiming