如何从json反序列化Celery画布?

时间:2016-10-14 21:35:51

标签: python json celery

很容易将Celery画布序列化为json,如下所示:

import json
from celery import Celery
from celery import chain

app = Celery()


@app.task
def add(a, b):
  return a + b


@app.task
def mult(a, b):
  return a * b


def main():
  canvas = chain(add.s(1, 2), mult.s(2), mult.s(2))
  print json.dumps(canvas)

if __name__ == '__main__':
  main()

这会产生:

{
    "chord_size": null,
    "task": "celery.chain",
    "subtask_type": "chain",
    "kwargs": {
        "tasks": [
            {
                "chord_size": null,
                "task": "__main__.add",
                "subtask_type": null,
                "kwargs": {},
                "args": [
                    1,
                    2
                ],
                "options": {},
                "immutable": false
            },
            {
                "chord_size": null,
                "task": "__main__.mult",
                "subtask_type": null,
                "kwargs": {},
                "args": [
                    2
                ],
                "options": {},
                "immutable": false
            },
            {
                "chord_size": null,
                "task": "__main__.mult",
                "subtask_type": null,
                "kwargs": {},
                "args": [
                    2
                ],
                "options": {},
                "immutable": false
            }
        ]
    },
    "args": [],
    "options": {},
    "immutable": false
}

但是,我不清楚如何将这些数据反序列化回Celery画布。我调查了kombu/serialization.pycelery/security/serialization.py,但这些都不是答案。

1 个答案:

答案 0 :(得分:0)

要反序列化Canvas签名,您可以将字典传递给签名方法。

import json
from celery import signature

serialization = json.dumps(canvas)
canvas = signature(json.loads(serialization))

文档:http://docs.celeryproject.org/en/latest/reference/celery.html#celery.signature