[英]How can I pass TemporaryUploadedFile to celery task?
我有一個代碼:
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 中。 然后 celery worker 可以從 redis 中獲取這個名稱並讀取文件。 但我不確定:是否可以在 celery worker 從 redis 獲取名稱之前刪除該文件?
一旦request_finished
信號被觸發, TemporaryUploadedFile
就會關閉並刪除。 當您的 Celery 工作人員訪問該文件時,該文件很可能不再存在。
您應該將文件復制到一個永久位置,並在完成后讓 Celery 清理文件。
def close(self):
try:
return self.file.close()
except OSError as e:
if e.errno != errno.ENOENT:
# Means the file was moved or deleted before the tempfile
# could unlink it. Still sets self.file.close_called and
# calls self.file.file.close() before the exception
raise
根據 TemporaryUploadedFile 的close
方法的源代碼,臨時文件被移動不會被關閉,所以你可以移動它並將它的新路徑傳遞給 celery 任務,然后在 celery 任務完成時自己刪除它。 這樣,您將節省將文件復制到永久位置的時間和資源。
import os
from django.core.files import uploadedfile
file = request.FILES["import_file"]
new_path = '/tmp/import_file'
if isinstance(file, uploadedfile.TemporaryUploadedFile):
os.rename(file.file.name, new_path)
else: # Deal with InMemoryUploadedFile
with open(new_path, 'wb') as f:
for chunk in file.chunks():
f.write(chunk)
task_id = importing.delay(new_path).id
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.