5

I have been working on a project built with Django. When I run profiler due to slowness of a page in project, this was a line of the result:

10 0.503 0.050 0.503 0.050 {method 'recv_into' of '_socket.socket' objects}

Which says almost 99% of passed time was for the method recv_into(). After some research, I learned the reason is Nagel's algorithm which targets to send packets only when the buffer is full or there are no more packets to transmit. I know I have to disable this algorithm and use TCP_NODELAY but I don't know how, also it should only affect this Django project.

Any help would be much appreciated.

Berkay
  • 1,029
  • 1
  • 10
  • 28
  • What did you exactly profile? Are you using the development server? – Selcuk Nov 13 '19 at 13:29
  • I have profiled a specific method in which data from database(using mongoengine) is being processed. I am not using the development server. The ones in use are: Python, Django, mongoengine and REST framework. – Berkay Nov 13 '19 at 14:03
  • 2
    Not a direct answer to your question but TCP_DELAY is only relevant when there are many small packets going back and forth, as in the case of a distributed game server etc. I take it that mongodb responses would be relatively bigger, so I don't think there will be a major (if any) performance gain here. – Selcuk Nov 13 '19 at 23:20
  • Thanks a lot. I'll try the way you suggested to see if there's any performance gain. – Berkay Nov 14 '19 at 06:42

4 Answers4

0

Not aware of django settings.I think you can do this at

load balancer(nginx) level rather than django, under http directive.

http://nginx.org/en/docs/http/ngx_http_core_module.html#tcp_nodelay

At uwsgi level:

https://uwsgi-docs.readthedocs.io/en/latest/Options.html#tcp-nodelay

drd
  • 575
  • 2
  • 8
0

Use nginx as a proxy and into the configuration you can set in the http block

http{
  #...
  tcp_nodelay   off;
  #...
}
Yugandhar Chaudhari
  • 3,831
  • 3
  • 24
  • 40
0

You can try with "gevent" , This can help us a little by improving socket performance by using cooperative threads.

Here below is the thread which will help you to combine Django and gevent , Which can give just the basics

from gevent import monkey; monkey.patch_all()
from gevent.wsgi import WSGIServer

from django.core.management import setup_environ    
import settings
setup_environ(settings)

from django.core.handlers.wsgi import WSGIHandler as DjangoWSGIApp
application = DjangoWSGIApp()
server = WSGIServer(("127.0.0.1", 1234), application)
print "Starting server on http://127.0.0.1:1234"
server.serve_forever()

Source :

how to combine django plus gevent the basics?

http://www.gevent.org/

redhatvicky
  • 1,912
  • 9
  • 8
-1

Are you using cache settings in the settings.py file? Please check that maybe you have tcp_nodelay enable there, if so then remove it or try to clear browser cache.

  • Thanks for your time for answering the question. There is nothing about caching in settings.py, so I don't think this is the answer. – Berkay Nov 13 '19 at 13:23