How exactly does a python (django) request happen? does it have to reparse all the codebase?

257 views Asked by At

With a scripting language like python (or php), things are not compiled down to bytecode like in .net or java.

So does this mean that on every request, it has to go through the entire application and parse/compile it? Or at least all the code required for the given call stack?

2

There are 2 answers

0
Alex Martelli On BEST ANSWER

With a scripting language like python (or php), things are not compiled down to bytecode like in .net or java.

Wrong: everything you import in Python gets compiled to bytecode (and saved as .pyc files if you can write to the directory containing the source you're importing -- standard libraries &c are generally pre-compiled, depending on the installation choices of course). Just keep the main script short and simple (importing some module and calling a function in it) and you'll be using compiled bytecode throughout. (Python's compiler is designed to be extremely fast -- with implications including that it doesn't do a lot of otherwise reasonable optimizations -- but avoiding it altogether is still faster;-).

0
Ignacio Vazquez-Abrams On

When running as CGI, yes, the entire project needs to be loaded for each request. FastCGI and mod_wsgi keep the project in memory and talk to it over a socket.