5

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.

jhagege
  • 1,486
  • 3
  • 22
  • 36

4 Answers4

7

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.

  • 1
    Thank you! This resolved it for me on macOS and saves me lots of time waiting – contmp Jun 06 '20 at 02:07
  • Lifesaver really. This instantly fixed it. In PyCharm, you can change the host used by editing the run configuration. – WayToDoor Mar 28 '23 at 08:20
1

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.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
  • Specific project only, thanks a lot for your answer. Do you know how could I debug? – jhagege Jan 11 '15 at 01:18
  • Try Django Debug Toolbar https://github.com/django-debug-toolbar/django-debug-toolbar as the first shot – Mikko Ohtamaa Jan 11 '15 at 01:27
  • Found it, Middleware that sends data to a remote monitoring service. Solution was to activate it only on production server and not in dev! – jhagege Jan 11 '15 at 01:39
0

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.

Alireza
  • 1,706
  • 16
  • 23
-7

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

jhagege
  • 1,486
  • 3
  • 22
  • 36