gpt4 book ai didi

python - celery store 在什么时候产生结果?

转载 作者:可可西里 更新时间:2023-11-01 11:24:15 25 4
gpt4 key购买 nike

我在 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

关于python - celery store 在什么时候产生结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52570489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com