johnny-cache
johnny-cache copied to clipboard
utils.celery_enable_all still needed with cache.enable() ?
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)
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
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.