rkern / line_profiler

(OLD REPO) Line-by-line profiling for Python - Current repo ->
https://github.com/pyutils/line_profiler
Other
3.6k stars 256 forks source link

'profile' is not defined exception when using line_profiler with django #154

Closed ramast closed 5 years ago

ramast commented 5 years ago

When running

kernprof -v -l ./manage.py runserver

I get the exception

  File "/project/src/tracking/serializers.py", line 91, in MapEntrySerializer
@profile
 NameError: name 'profile' is not defined

Inspired by this, I've solved the problem by running Django server like that

kernprof -v -l ./manage.py runserver --noreload

works flawlessly now. Creating that issue in case someone else encountered this problem.

Gabriel-Faivre commented 5 years ago

Thanks for the help. Sorry to comment on a closed issue but can you explain why the noreload switch makes it work?

ramast commented 5 years ago

Basically ./manage.py starts a separate process to run your code into while it monitor the code for any changes. when a change is detected, it restart that separate process.

line_profiler has no access to that separate process and can't inject it's code in it.

When adding --no-reload, manage.py will no longer need to monitor anything and will run your code directly in same process and that's why it works.

That's my understanding anyway, could be wrong