需要从celery共享任务队列进程中返回对象

发布于 2025-01-13 06:18:15 字数 670 浏览 3 评论 0原文

遇到了错误“kombu.exceptions.EncodeError:'JsonResponse'类型的对象不是 JSON 可序列化的”

我在 settings.py 中

CELERY_BROKER_URL = 'amqp://localhost'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_BACKEND = 'django-db'

:因此它只接受 json 值形式的响应。但我需要返回对象或实例 示例:我需要返回文件路径。提前致谢。

@shared_task
def celery_upload(path, id , user_id):
    upload = Upload()
    result = upload.file_generation_validation(path, id , user_id)
    excel = Excel()
    file_path = excel.Export(result["error"], "aaa")
    return file_path

如果我返回结果,它将成功,但 file_path 对象会抛出错误。

I have faced the error "kombu.exceptions.EncodeError: Object of type 'JsonResponse' is not JSON serializable "

In my settings.py :

CELERY_BROKER_URL = 'amqp://localhost'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_BACKEND = 'django-db'

So it only accepts response as a json value. But i need to return the object or instance
Example : I need to return file path. Thanks in advance.

@shared_task
def celery_upload(path, id , user_id):
    upload = Upload()
    result = upload.file_generation_validation(path, id , user_id)
    excel = Excel()
    file_path = excel.Export(result["error"], "aaa")
    return file_path

if I return result it will be successful, But file_path object throws error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与往事干杯 2025-01-20 06:18:15

尝试使用 pickle 序列化格式而不是 JSON,那么您应该能够发送/接收复杂的对象。

请检查以下参考资料
https://docs.celeryproject.org/projects/kombu/ en/stable/userguide/serialization.html

https://coderbook.com/@marcus /如何将复杂数据类型传递到celery-tasks/

Try pickle serialization format instead of JSON then you should be able to send/receive complex objects.

Please check the below references
https://docs.celeryproject.org/projects/kombu/en/stable/userguide/serialization.html

https://coderbook.com/@marcus/how-to-pass-complex-data-types-to-celery-tasks/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文