Psyco and Django
Psyco is a module that optimizes Python applications on the fly. Numerous resources online describe how to use psyco in a Django-powered application to speed it up.
My experiences with this has been less than wonderful. I wrote a simple middleware class to import psyco as suggested here:
# Be sure to only load on the proper architecture from platform import architecture if not settings.DEBUG and architecture()[0] == '32bit': if architecture()[0] == '32bit': try: import psyco except ImportError: pass class PsycoMiddleware(object): def process_request(self, request): # Do not waste time trying to optimize the re module psyco.cannotcompile(re.compile) # Limit memory usage psyco.profile(memory=2048) return None
In development this appeared to work fine (we excluded the if not settings.DEBUG portion while testing). Once we were up and running on the production server, however, mysterious exceptions began to surface.
In particular, we seemed to be “missing” on some page hits. Specifically, we would get TemplateDoesNotExist exceptions when the templates did, in fact, exist. We were never able to sort that out. They immediately stopped when we commented out the psyco-related stuff. We tried changing the memory usage, excluding more items from compilation (including the Django loader classes), all to no avail.
My recommendation is avoidance of psyco in a Django setting. If someone has an idea what might have caused this, I’d love to hear an explanation.
Tags:django, python





8. June 2009 at 12:38 pm :
It’s been over a year since your original post — has the state of psyco+django changed at all? It seems like over the last year, both projects have matured greatly, and was wondering if some of these mysterious issues have been solved…
11. June 2009 at 6:38 am :
Not that I am aware of.