Reputation: 109
So as I understood Django framework and python language are synchronous which means things are processed sequentially and only one user can be served at each particular moment.
In that case what happens when a single user tries to upload relatively large file to server. Does the whole web-app become unresponsive for everyone else?
What is the right way to do the file upload in Python anyways?
Upvotes: 0
Views: 272
Reputation: 422
No, Django is a Web Framework. Its doesn't get unresponsive when you upload file. Django is run as a WSGI application. Django's request handler is thread-safe.
By default, if an uploaded file is smaller than 2.5 megabytes, Django will hold the entire contents of the upload in memory. However, if an uploaded file is too large, Django will write the uploaded file to a temporary file stored in your system’s temporary directory.
To upload file you can use this code
def upload_file(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
handle_uploaded_file(request.FILES['file'])
return HttpResponseRedirect('/success/url/')
else:
form = UploadFileForm()
return render(request, 'upload.html', {'form': form})
def handle_uploaded_file(f):
with open('some/file/name.txt', 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
Source
This is old but will give you overview http://www.b-list.org/weblog/2006/jun/13/how-django-processes-request/
Upvotes: 1