Psyco and Django
posted on 02/15/08 at 4:25 p.m. | django python

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.

Technorati Technorati

Post a comment