sharpek.net " High "django gizmodo
To commit this entry inspired me to publish a book highperformancedjango. Price $ 40 for 150 pages is to me quite a lot. So I decided to write something from each other about django and on how to speed up its performance.
On the internet you can find a lot of web frameworks benchmark. Unfortunately, doing these people do not have much experience gizmodo with all tested frameworks. The result is monsters in which someone to test the performance pyramid / flask / django uses outdated and free mod_python. In my comparison of performance django / pyramid / flask / ror / symfony is pointless, because as soon as it industrialized we draw from each comparable values (question at what cost). In this entry I will explain a few things about the django environment.
Applications written in python can be served in different ways. Some of this will use apache + mod_wsgi and others gunicorn gizmodo + nginx as a reverse proxy. You can also fire up uwsgi directly on port 80, as one who likes. According to me, the most efficient and at the same time not nascent major problems is to use nginx + uwsgi as upstream.
Currently, the best solution gizmodo is to use Postgres as a database server. In addition to the various specific fields, you can use tools like pgBouncer and pgPool which can considerably speed up our application. Of course, if we do not have the benefit of such tools should gizmodo consider the inclusion of persistent connections gizmodo in our application. Templates
Contrary to what you read django has a very primitive, immature, and generally hopeless template system. In addition, it operates on a fairly slow. Fortunately, you can speed up its operation, including cache by modifying TEMPLATE_LOADERS.
This solution has one rather large flaw - every time we make changes to the templates will be required gizmodo to restart the application (which is why I do not recommend to use it on an environment gizmodo other than production).
If I start to write an application from scratch is worth considering the use of another system Jinja2 templates. Jinja2 templates except that they are much faster, solve too many problems of syntax. Sessions
By default, a session in django based on a database, which is a fairly free solution. Quite popular is change change ENGINE table django_session to MEMORY. However, please note that sessions gizmodo Old sessions are not automatically deleted, so every now and then our application will treat us this error: "TABLE IS FULL".
The main difference is that cached_db also holds sessions in the database, so that even when the cache wyczyścimy our application, registered users will not be logged out. Only since version 1.7 allows gizmodo you to define SESSION_CACHE_ALIAS django.
The best option for a session in django IMO is to use Redis server as a database for the session. The solution is two things, gizmodo redis server and install the library django-redis-session. With this solution I use for a long time, and unlike previous solutions, I had no problem with that-hungry. CACHE appropriate backend
Django supports various mechanisms for caching. The best solution is to use memcache. Django "natively" supports two libraries for memcache: django.core.cache.backends.memcached.PyLibMCCach django.core.cache.backends.memcached.MemcachedCache
A better solution is to use PylibMC gizmodo (library written in C). It should also be familiar with what the library offers external gizmodo django-pylibmc (allows for greater gizmodo client configurations gizmodo pylibmc gizmodo assurances that the author is much faster).
If your application uses a very large extent from the cache, it is worth considering the use of twemproxy, particularly when servers memcache / redis are on other machines. Cache at the web server (nginx)
Despite the fact that we are using memcache, our application will continue gizmodo to get inquiries (by which uwsgi processes will be blocked). If you have views that are very popular to consider the use of two-Level cache. With this treatment we will be able to handle more of a request at the level of our application (the rest will was coming directly from the web server), so what we do in internetowykantor.pl.
In django set the key that is the same as the URL for the view, and configure nginx server so that for every query checks if there is a key in memcache if the key exists then the server returns directly to its value.
location / api {default_type text / html; memcached_key set $ 1: $ uri; memcached_pass unix: / tmp / memcache- 1; error_page 404 502 504 = @ app; } When not to use the cache?
Well, sometimes it's better to do select_related, sometimes gizmodo not - write about it in the next entry in the