I'm using django-celery and I'd like to set the TASK_SERIALIZER
to JSON instead of pickle.
I can do this on a per-method basis by changing my task decorators from
@task
to
@task(serializer="json")
But I'd like to do it globally. Setting
TASK_SERIALIZER="json"
in settings.py
doesn't work. Trying to run
import celery
celery.conf.TASK_SERIALIZER="json"
(as implied here) results in
AttributeError: 'module' object has no attribute 'conf'
Any idea how to configure this setting when running celery through django?
Figured it out.
In settings.py
you need to set
CELERY_TASK_SERIALIZER = "json"
Docs are confusing, at least to me.
I found that creating a celeryconfig file (like the docs recommend) makes things a lot cleaner.
celeryconfig.py
# Celery configuration file
BROKER_URL = 'amqp://'
CELERY_RESULT_BACKEND = 'amqp://'
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'America/Los_Angeles'
CELERY_ENABLE_UTC = True
You can sent set it with this command (once you call Celery)
celery.config_from_object('celeryconfig')
From the doc :
For the task messages you can set the CELERY_TASK_SERIALIZER setting to “json” or “yaml” instead of pickle. There is currently no alternative solution for task results (but writing a custom result backend using JSON is a simple task)
So setting CELERY_RESULT_SERIALIZER = "json"
looks useless. In my case, results are still in pickle (Celery 3.1.3). Yeah. I know...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With