Wednesday, December 10, 2008
Save CPU Usage! Dont Use StringIO
After enabling the python profiler, I noticed that StringIO adds about 500 CPU usage to your process so ignoring the use of it will save a bit of cpu usage.
After enabling the python profiler, I noticed that StringIO adds about 500 CPU usage to your process so ignoring the use of it will save a bit of cpu usage.
Decorators allow developers to use aspect oriented approach. Here is an example of a custom python/django decorator that checks if the user is a super user and redirects it to login page if not
def super_user(func):Here is an example usage:
def call(request, *args, **kwds):
user = request.user
if user.is_authenticated() and user.is_superuser:
return func(request, *args, **kwds)
else:
return utils.redirect(settings.LOGIN_URL)
return call
@super_user
def my_view(request):
pass
While playing with django debug toolbar, I noticed that some page on my application causes serious amount of queries that is being executed. One page actually executes around 580 queries so I investigated and noticed that this was caused probably by lazy initialization, not using select_related() on my queries and i'm caching QuerySet object instead of list.
An example of a getter function on my model:
@property
def data(self):
cache_data = getattr(self, 'cache_data', None)
if cache_data:
return cache_data
else:
self.cache_data = TimecardData.objects.filter(timecard_day=self)
return self.cache_data
@propertyThe queries was reduced from 560 to 300 queries.
def data(self):
cache_data = getattr(self, 'cache_data', None)
if cache_data:
return cache_data
else:
self.cache_data = list(TimecardData.objects.select_related()
.filter(timecard_day=self))
return self.cache_data
@propertyThe code above to this one checks using "if cache_data" but since empty list returns false if evaluated, it queries the database again instead of using cache. This one brought the queries from 580 to 120 (a very big difference)
def data(self):
cache_data = getattr(self, 'cache_data', None)
if cache_data != None:
return cache_data
else:
self.cache_data = list(TimecardData.objects.select_related().filter(timecard_day=self))
return self.cache_data
I'm not sure If I'm the only one who feel that the date/time module in python 2.5 is very hard to use. If any of you ever tried using joda time, you'll notice that it's way to easy compared to python's date/time library.
Correct me If i'm wrong but joda time was created by Google guys so I hope that in the near future, python will have a better date/time library (since Google is the main supporter of python).
As I was browsing, I came across an article used to host python web applications better. The module called 'mod_wsgi' is a new&better apache module than mod_python when hosting django/python apps. I haven't personally tried it (maybe I will this coming weekend) but the comments by other people are very interesting. Anyway to give you some short round-up, this are the things that it can do:
Links:
http://code.google.com/p/modwsgi/
http://www.technobabble.dk/2008/aug/25/django-mod-wsgi-perfect-match/
http://code.djangoproject.com/wiki/django_apache_and_mod_wsgi
| <Page 1 of 3> | Next Page |
