Django在上传文件时给出502 Bad Gateway错误

时间:2018-08-23 03:58:40

标签: python django file-upload

我有一个下面定义的文件上传表格

  <form method="post" enctype="multipart/form-data", action="/api/file_upload" style="padding: 15px;">
    <label style="display: table-cell;">Upload</label>
    <input type="file" name="file">
    <div class="container-fluid" style="padding:5px">
    <button type="submit" style="width:100px;border-radius:50px;font-family: NunitoSans-Regular;height:20px;background-color:#fff;border:1px solid rgb(66, 66, 66);color:black;padding: 0px;" class="btn btn-primary pull-right">Upload</button>
    </div>
  </form>

这是基于定义的操作的urls.py文件

url(r'^api/file_upload$', ui_server.store_file, name='store_file')

这是为文件上传定义的功能

@csrf_exempt
def store_file(request):
    if request.method == "POST":
        logged_in_user = request.user.username
        # now get the uploaded file
        client_file = request.FILES['file']
        file_path = /home/ec2-user/uploads/ + "" + org + "/"
        print("client file name", client_file.name)
        # the file is going to be an instance of UploadedFile
        try:
            with open(file_path + '%s' % client_file.name, 'wb+') as dest:
                for chunk in client_file.chunks():
                    dest.write(chunk)
                    # return success message
            print("file created")
            return HttpResponse('Success')
        except Exception as e:
            print(e)
            return HttpResponse('Failure')

        finally:
            # process the client file
            result = read_client_file()
            print(result)

现在,我可以上传文件并将其存储在适当的路径中了。但是功能read_client_file()会花费一些时间来读取文件并进行处理。这就是为什么我定义了一个成功上传文件的返回语句的原因并在finally块中继续进行处理。

但这无济于事,它开始处理文件。现在,由于花费了很长时间,django认为它没有对此URL做出响应,并抛出了502错误。

我有一个django + gunicorn + nginx设置。当检查终端时,出现以下错误

[ec2-user@ip-122-12-13-51 Utora]$ [2018-08-23 03:54:56 +0000] [6036] [CRITICAL] WORKER TIMEOUT (pid:6049)

我在做什么错了?

0 个答案:

没有答案