Very slow first request in Django Development server

5.2k Views Asked by At

Whenever I run python manage.py runserver and accesses my website with localhost:8000 in the browser, the first request is VERY slow (around 10 seconds).

Following requests are really fast. Is there any way to improve the performance of this? It's very unconvenient because while in development, it's always reloading due to updates to the code.

Thanks a lot.

4

There are 4 best solutions below

3
On BEST ANSWER

Found it, Middleware that sends data to a remote monitoring service. Solution was to activate it only on production server and not in dev!

3
On

Does this happen to empty Django project (freshly generated) or just for your specific site?

Without having more specific information it is hard to tell why.

The first request most likely lazily initializes some data. It could e.g. call external websites to get data and is slow due to bad DNS or slow Internet connection. Alternative, caches are empty and they are repopulated on the startup of the Django development server.

2
On

You will have this kind of issue if you start runserver on 0.0.0.0 instead of 127.0.0.1 since it has to resolve some strange route interface loop.

0
On

Use django-debug-toolbar ,django-debug-toolbar is a very handy tool that provides insights into what your code is doing and how much time it spends doing it. In particular it can show you all the SQL queries your page is generating, and how long each one has taken.