需要从celery共享任务队列进程中返回对象
遇到了错误“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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用 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/