celery store 在什么时候产生结果?
At what point does celery store result?
我在 Django 网络应用程序中使用 celery + redis 执行任务。问题是我不知道 celery 在什么时候将它的任务结果作为 JSON 对象存储到数据库中。之后出现了一个问题,因为它不能将 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 序列化。这就是为什么我在定义 dist_matrix
变量时使用 .tolist
方法。
问题是,celery 在什么时候存储它的变量,我如何存储来自 numpy 数组的信息作为任务结果?
Celery 需要序列化任务的参数。希望同样的问题有所帮助:
Django Celery send register email do not work
我在 Django 网络应用程序中使用 celery + redis 执行任务。问题是我不知道 celery 在什么时候将它的任务结果作为 JSON 对象存储到数据库中。之后出现了一个问题,因为它不能将 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 序列化。这就是为什么我在定义 dist_matrix
变量时使用 .tolist
方法。
问题是,celery 在什么时候存储它的变量,我如何存储来自 numpy 数组的信息作为任务结果?
Celery 需要序列化任务的参数。希望同样的问题有所帮助: Django Celery send register email do not work