我有一个代码:
def post(self, request, *args, **kwargs):
file = request.FILES["import_file"]
# create a tast with celery and save ID of the task
task_id = importing.delay(file).id
return Response({"task_id": task_id}, content_type="application/json")
当 type(file) 为 TemporaryUploadedFile 时,出现错误,因为文件无法写入 redis。
我可以获取这个临时文件的名称并将该名称保存到 Redis 中。然后celeryworker可以从redis中获取这个名称并读取文件。但我不确定:在 celery worker 从 redis 获取其名称之前可以删除该文件吗?
最佳答案
TemporaryUploadedFile
一旦request_finished
被关闭并删除。信号被发射。当您的 Celery 工作人员访问该文件时,该文件很可能不再存在。
您应该将文件复制到持久位置,并在完成后让 Celery 清理该文件。
关于python - 如何将 TemporaryUploadedFile 传递给 celery 任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33920073/