Djangocon: Give your pony wings - Benjamin WohlwendΒΆ

Tags: django, djangocon

(One of the summaries of a talk at the 2015 Djangocon EU conference).

Benjamin Wohlwend confesses that he’s a lazy programmer. He also likes his code to be lazy: he doesn’t want his code to do too much. Why? Code that doesn’t do much is code that is fast.

If you have performance problems, how do you find them? He uses django-debug-toolbar and django-devserver a lot.

django-debug-toolbar is great, especially because it lists the number of queries (which is a common performance problem).

django-devserver also prints out the number of queries and the time it took. For every request. Handy. It is a runserver replacement.

To reduce the number of queries, django provides select_related() and prefetch_related() Select_related is for immediately grabbing related objects in one big query. prefetch_related does something similar, but it is better suited for many-to-many fields and reverse foreign keys.

There are many more tricks. You can use django’s annotate function to let the database count numbers of related objects instead of grabbing them all. Much quicker. Also .defer() helps, this lets you tell django not to grab certain columns as they’re not needed.

Caching can make your code way faster. But cache invalidation is hard. It often makes your code harder to test and harder to reason about. So only use it when you really really need it.

An easy way is to use the @cache_page decorator. And template caching with {% cache %}. And ORM caching with johnny-cache or django-cache-machine.

If you need more, you can look at a caching HTTP proxy like Varnish.

Highly recommened: read the “high performance django” book.

In summary:

  • Watch out for those SQL queries.
  • Being lazy is good, don’t let your code do too much.
  • If everything else fails, bring out the big caching guns.
Photo from our 2014 cycling holiday
blog comments powered by Disqus
 
vanrees.org logo

About me

My name is Reinout van Rees and I work a lot with Python (programming language) and Django (website framework). I live in The Netherlands and I'm happily married to Annie van Rees-Kooiman.

Weblog feeds

Most of my website content is in my weblog. You can keep up to date by subscribing to the automatic feeds (for instance with Google reader):