1

I tried using the Python tool cProfile. If I do unit tests on my files (I usually have a set of test routines if the module is invoked from the CLI in python) then doing

python -m cProfile -o outputfilename myfile.py

gives me meaningful output I can examine from within the Python interactive mode after importing cprofile and doing the usual stuff, leading to

stats.print_callers('foo')

giving meaningful reports about functions with 'foo' in them.

However, if I try to do this with the Django server, by doing

python -m cProfile -o outputfilename manage.py runserver

and using the web app, I get a load of stuff in the file...but I can't find the references to my actual code/function calls. All of the profiling seems to be loaded with endless Django internals.

In fact, it looks to me like Django uses the builtin exec() function a whole lot, and I am led to believe my code is getting executed via that path and doesn't get captured.

First off, is this how Django works on the development server, and is it the same for production? That seems like using exec() a lot is a major potential security issue.

But, more important, how can I profile the total time, number of calls, etc., to my code - and not all these Django internals?

eSurfsnake
  • 647
  • 6
  • 17
  • I suspect this is due to runserver being multi process and you're only profiling the top process. https://stackoverflow.com/questions/11041683/python-multiprocess-profiling – rrauenza Feb 05 '21 at 20:47

0 Answers0