Uploaded file performance

24 views
Skip to first unread message

kern zhou

unread,
Nov 13, 2020, 1:44:38 AM11/13/20
to Django users
Hi,

I am using the django to handle the file upload.
In the view implementation, I am trying to write the uploaded file to disk, but it causes bad performance because writing the uploaded file to disk spend lots of time.
Is any better solution to handle the file upload? Maybe create a detach thread to handle the file writing

The below is the simplified view implementation:

```
def upload_file(request): 
  if request.method == 'POST': 
           form = UploadFileForm(request.POST, request.FILES) 
            if form.is_valid():                                      
                  with open('some/file/name.txt', 'wb+') as destination: 
                           for chunk in f.chunks(): 
                                   destination.write(chunk)
            return HttpResponseRedirect('/success/url/') 
  else
          form = UploadFileForm() 
          return render(request, 'upload.html', {'form': form})  
```
Reply all
Reply to author
Forward
0 new messages