Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure TASK_SERIALIZER with django-celery

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?

like image 685
Leopd Avatar asked Jul 08 '11 17:07

Leopd


3 Answers

Figured it out.

In settings.py you need to set

CELERY_TASK_SERIALIZER = "json"

Docs are confusing, at least to me.

like image 107
Leopd Avatar answered Nov 14 '22 08:11

Leopd


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')
like image 6
Jarie Bolander Avatar answered Nov 14 '22 08:11

Jarie Bolander


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...

like image 1
Stan Avatar answered Nov 14 '22 06:11

Stan