Code change monitoring malfunctioning with Django

2020-05-28 01:37发布

This is an issue that I've encountered on all my sites that are running Django 1.7 in mod_wsgi. The nub of the issue is that if, while developing locally, I introduce a fatal error into the codebase, and then subsequently correct it, the code monitoring script doesn't detect the correction.

I use Graham Dumpleton's monitor.py script to detect changes to the codebase when I'm developing locally (I use apache rather than the Django development server).

It always used to work in Django <= 1.6, but in Django 1.7 I get the following error:

File "/home/me/.virtualenvs/myvirtualenv/lib/python2.7/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
    django.setup()
File "/home/me/virtualenvs/myvirtualenv/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
File "/home/me/.virtualenvs/myvirtualenv/lib/python2.7/site-packages/django/apps/registry.py", line 78, in populate
    raise RuntimeError("populate() isn't reentrant")
RuntimeError: populate() isn't reentrant

The irritating thing is that if I correct the error, monitor.py doesn't detect the change, so I have to either restart apache, or touch another file that was already loaded (e.g. the settings file).

I think this is because of the fact that "the reloading code only monitors imported files (aka sys.modules)" (source). So because the incorrect file wasn't successfully imported, monitor.py doesn't know to restart the process.

1条回答
戒情不戒烟
2楼-- · 2020-05-28 02:26

I am not sure what your deployment process is nor your production operating system, but in the Linux/Ubuntu world there is a OS command called pyclean. During my Django/Python deployment scripts (usually via fabric) I issue the command "pyclean ." in the project root. This script recursively deletes all .pyc files starting in the current folder. I hope this helps.

查看更多
登录 后发表回答