Version 10 (modified by Mrts, 17 years ago) ( diff )

--

class PsycoMiddleware(object):
    """
    This middleware enables the psyco extension module which can massively
    speed up the execution of any Python code.
    """
    def process_request(self, request):
        try:
            import psyco
            psyco.profile()
        except ImportError:
            pass
        return None

Note that you can use psyco.full() instead of psyco.profile(). See http://psyco.sourceforge.net/psycoguide/node8.html

Note that Psyco should only be used on 32bit systems. See http://www.alrond.com/en/2007/jan/25/performance-test-of-6-leading-frameworks .

One possible way to determine the architecture automatically:

from platform import architecture

if architecture()[0] != '32bit':
    raise Exception("Don't use this on non-32-bit platforms")

class PsycoMiddleware(object):
    """
    This middleware enables the psyco extension module which can massively
    speed up the execution of any Python code.
    """
    def process_request(self, request):
        try:
            import psyco
            psyco.profile()
        except ImportError:
            pass
        return None

One more issue I have with this -- why is import done on every request as opposed to module load time? The following looks saner to me:

from platform import architecture

if architecture()[0] != '32bit':
    raise Exception("Don't use this on non-32-bit platforms")

# let ImportError propagate at module load time so that people can notice and fix it
import psyco

class PsycoMiddleware(object):
    """
    This middleware enables the psyco extension module which can massively
    speed up the execution of any Python code.
    """
    def process_request(self, request):
        psyco.profile()
        return None

Or even (that's how I use it, and according to psyco.log() it seems to work)

import psyco
# psyco.log() -- uncomment this if you want to see how psyco performs
psyco.cannotcompile(re.compile)
psyco.profile()


class PsycoMiddleware(object):
    pass

See http://psyco.sourceforge.net/psycoguide/tutknownbugs.html#tutknownbugs for cannotcompile().

Note: See TracWiki for help on using the wiki.
Back to Top