【问题标题】:At what point does celery store result?芹菜商店在什么时候产生结果?
【发布时间】:2019-03-05 08:18:22
【问题描述】:

我在 Django Web 应用程序中使用 celery + redis 执行任务。问题是我不知道 celery 在什么时候将其任务结果作为 JSON 对象存储到 DB 中。之后出现了一个问题,因为它无法将 np 数组存储为 JSON。

import numpy as np
from scipy.sparse import dok_matrix


@shared_task(name="run_shortest_path_on_warehouse")
def run_shortest_path_on_warehouse(adjacency_matrix):
    sparse_matrix = dok_matrix(adjacency_matrix)
    dist_matrix = dijkstra_algorithm(sparse_matrix).tolist()
    return {'optimal_distance_matrix': dist_matrix}

Celery 无法存储结果,因为这个异常:

{"exc_type": "EncodeError", "exc_message": ["TypeError(\"Object of type 'ndarray' is not JSON serializable\",)"], "exc_module":    "kombu.exceptions"}

我知道 numpy 数组不仅仅是 JSON 可序列化的。这就是我使用.tolist 方法定义dist_matrix 变量的原因。

问题是,celery 在什么时候存储它的变量以及如何将 numpy 数组中的信息存储为任务结果?

【问题讨论】:

    标签: python django numpy redis celery


    【解决方案1】:

    Celery 需要序列化任务的参数。希望同样的问题有所帮助: Django Celery send register email do not work

    【讨论】:

    • 我不知道这是否是问题所在,但它确实有效。我建议所有人重新启动他们的 celery worker,因为这也可能是原因。
    猜你喜欢
    • 1970-01-01
    • 2018-08-24
    • 2019-02-17
    • 1970-01-01
    • 2018-06-11
    • 2014-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多