8

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.

seddonym
  • 16,304
  • 6
  • 66
  • 71
  • This is similar to how the python interactive interpreter cannot live reload perfectly. There is a copy of the code in the memory anyways and in many cases deleting the `.pyc/.pyo` files don't work. We fixed this error by putting a watcher on all files and reloading the apache entry point (`wsgi`) on change. This has a slight lag, and you can switch it off for production. – kunl Oct 26 '15 at 14:02
  • something similar happened to me using uwsgi. I then started using need-app=true. This makes uwsgi discard the app because it is not correctly loading. So once you develop the new change it will work. Maybe you can find something similar. – Manel Clos Nov 17 '15 at 17:40
  • 1
    I've ended up switching to the Django development server for local development. Unless you've got a really good reason, I'd say it's a much better approach than working with Apache & mod_wsgi for local Django development. – seddonym Feb 05 '16 at 10:13
  • Is there a question here? I can see a complaint :-) but not a question that would have a correct answer. On the other hand, if you *do* have an answer, please provide that as an answer and accept it. – bignose Nov 30 '16 at 23:38

1 Answers1

0

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.

drsnark
  • 2,813
  • 2
  • 15
  • 19