Celery:有没有办法编写自定义JSON编码器/解码器?

时间:2014-02-07 15:41:12

标签: python json celery

我想在我的应用程序中向celery任务发送一些对象。这些对象显然不是使用默认json库的json序列化。有没有办法让芹菜使用自定义JSON Encoder / Decoder序列化/反序列化这些对象?

1 个答案:

答案 0 :(得分:40)

这里有点晚了,但您应该可以通过在kombu序列化程序注册表中注册来定义自定义编码器和解码器,如文档中所示:http://docs.celeryproject.org/en/latest/userguide/calling.html#serializers

例如,以下是Django的自定义日期时序列化器/反序列化器(子类python's builtin json module):


myjson.py (将其放在settings.py文件的同一文件夹中)

import json
from datetime import datetime
from time import mktime

class MyEncoder(json.JSONEncoder):   
    def default(self, obj):
        if isinstance(obj, datetime):
            return {
                '__type__': '__datetime__', 
                'epoch': int(mktime(obj.timetuple()))
            }
        else:
            return json.JSONEncoder.default(self, obj)

def my_decoder(obj):
    if '__type__' in obj:
        if obj['__type__'] == '__datetime__':
            return datetime.fromtimestamp(obj['epoch'])
    return obj

# Encoder function      
def my_dumps(obj):
    return json.dumps(obj, cls=MyEncoder)

# Decoder function
def my_loads(obj):
    return json.loads(obj, object_hook=my_decoder)


settings.py

# Register your new serializer methods into kombu
from kombu.serialization import register
from .myjson import my_dumps, my_loads

register('myjson', my_dumps, my_loads, 
    content_type='application/x-myjson',
    content_encoding='utf-8') 

# Tell celery to use your new serializer:
CELERY_ACCEPT_CONTENT = ['myjson']
CELERY_TASK_SERIALIZER = 'myjson'
CELERY_RESULT_SERIALIZER = 'myjson'