简体   繁体   中英

Django / Apache returns HTTP 500 Randomly

I have been dealing with a rather strange issue lately. My backend has httpd + mod wsgi + Django setup.

I have a class-based view as follows:

class ExtrasView(View):

def get(self, request):
    path = settings.BASE_DIR + "/data.json"
    with open(path, encoding='utf-8') as f:
        data = json.loads(f.read())
    return JsonResponse(data)

The get request to the above view works perfectly 9 out of 10 times. However, randomly this view would give a response with status 500. Based on the apache log, it seems that the response body length is correct ie the length of data in the file. This is verified by apache access logs.

Does anyone have an idea why this might be happening? I have checked the error logs and there's nothing in the errorlog. The error log does print tracebacks in case of exceptions or other syntax errors, just in this case it doesn't print anything so I'm clueless. Needless to say, the file being read is a static file that 100% exists.

The data in the file is huge, it's length is about 30-40k characters. Would this cause an issue? If yes, then why does it work 9 out of 10 times?

Any comments are welcome.

Why dont you just return the json file directly instead of loading it as json and returning it. Something like

def get(self, request)
    with open(path, encoding='utf-8') as f:
        return Response(f.read())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM