views:

162

answers:

4
+3  Q: 

Python accelerator

I'm planning to use Python to develop a web application. Anybody has any idea about any accelerator for python? (something like eAccelerator or apc for php) if not, is there any way to cache the pre-compiled python bytecode ? Any idea about the performance comparison between python and php (assuming db/network latencies are same)

Thanks in advance.

+3  A: 

The compiled python Bytecode is cached in .pyc files automatically in every environment i have seen. There is so need to do anything else as far as i know.

If you want to generate these files directly you can use: http://docs.python.org/library/py_compile.html

edorian
+5  A: 

As long as you do trivial amounts of work in your "main script" (the one you directly invoke with python and which gets a __name__ of __main__) you need not worry about "caching the pre-compiled python bytecode": when you import foo, foo.py gets saved to disk (same directory) as foo.pyc, as long as that directory is writable by you, so the already-cheap compilation to bytecode happens once and "forever after" Python will load foo.pyc directly in every new process that does import foo -- within a single process, every import foo except the first one is just a fast lookup into a dictionary in memory (the sys.module dictionary). A core performance idea in Python: makes sure every bit of substantial code happens within def statements in modules -- don't have any at module top level, in the main script, or esp. within exec and eval statements/expressions!-).

I have no benchmarks for PHP vs Python, but I've noticed that Python keeps getting optimized pretty noticeably with every new release, so make sure you compare a recent release (idealy 2.7, at least 2.6) if you want to see "the fastes Python". If you don't find it fast enough yet, cython (a Python dialect designed to compile directly into C, and thence into machine code, with some limitations) is today the simplest way to selectively optimize those modules which profiling shows you need it.

Alex Martelli
+7  A: 

There's a trick to this.

It's called mod_wsgi.

The essence of it works like this.

  1. For "static" content (.css, .js, images, etc.) put them in a directory so they're served by Apache, without your Python program knowing they were sent.

  2. For "dynamic" content (the main HTML page itself) you use mod_wsgi to fork a "back-end" process that runs outside of Apache.

This is faster than PHP because now several things are going on at once. Apache has dispatched the request to a backend process and then moved on to handle the next request while the backend is still running the first one.

Also, when you've sent your HTML page, the follow-on requests are handled by Apache without your Python program knowing or caring what's going on. This leads to huge speedups. Nothing to do with the speed of Python. Everything to do with the overall architecture.

S.Lott
Link: http://code.google.com/p/modwsgi/
dkamins
+2  A: 

Others have mentioned Python byte code files, but that is largely irrelevant. This is because hosting mechanisms for Python, with the exception of CGI, keep the Python web Application in memory between requests. This is different to PHP which effectively throws away the application between requests. As such, Python doesn't need an accelerator as the way Python web hosting mechanisms work avoids the problems that PHP has.

Graham Dumpleton