【发布时间】: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