celery 任务将 python 列表转换为字符串。如何避免这种情况?

celery tasks converts python list into strings. how to avoid this?

我在 celery 共享任务中有一个电子邮件发送任务 运行。我正在将 python 列表传递到 html 模板中。但芹菜将此列表转换为字符串。但在不使用 celery task 的情况下,我得到了实际列表。

docs你可以读到:

Data transferred between clients and workers needs to be serialized, so every message in Celery has a content_type header that describes the serialization method used to encode it.

The default serializer is JSON, but you can change this using the task_serializer setting, or for each individual task, or even per message.

作为序列化程序,您还可以使用 pickleyamlmsgpack,但数据必须序列化。

结论:

序列化是强制性的,请记住任务将使用 JSON 进行通信(这是您的情况)并在使用前反序列化数据。