小编典典

Django停止使用RuntimeError:populate()未重入

django

我一直在开发使用WSGI部署在Apache服务器上的Django Web应用程序,并且一切进展顺利。今天,我对应用程序进行了一些较小的更改admin.py,以尝试自定义内置的Django Admin接口,并且最初出现了语法错误(括号未封闭)。这意味着当我触摸wsgi.py并加载代码时(我的虚拟主机上的WSGI以守护程序模式运行),我的网站被内部服务器错误替换,因为WSGI遇到语法错误时就停止了。

因此,我更正了语法错误,并检查了我是否再也没有了manage.py check,并wsgi.py尝试重新部署。但是我的网站仍然显示内部服务器错误!检查Apache日志,这就是我看到的内容:

[Sun Nov 23 13:52:46 2014] [info] mod_wsgi (pid=19093): Create interpreter 'quotes.cs.cornell.edu|'.
[Sun Nov 23 13:52:46 2014] [info] mod_wsgi (pid=19093): Adding '/extra/www/html/quotes/quotes_django' to path.
[Sun Nov 23 13:52:46 2014] [info] mod_wsgi (pid=19093): Adding '/opt/rh/python27/root/usr/lib64/python2.7/site-
packages/' to path.
[Sun Nov 23 13:52:46 2014] [info] [client 128.84.33.19] mod_wsgi (pid=19093, process='quotes.cs.cornell.edu',
  application='quotes.cs.cornell.edu|'): Loading WSGI script '/extra/www/html/quotes/quotes_django/quotes_django/
wsgi.py'.
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] mod_wsgi (pid=19093): Target WSGI script '/extra/www/html/
quotes/quotes_django/quotes_django/wsgi.py' cannot be loaded as Python module.
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] mod_wsgi (pid=19093): Exception occurred processing WSGI
script '/extra/www/html/quotes/quotes_django/quotes_django/wsgi.py'.
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] Traceback (most recent call last):
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]   File "/extra/www/html/quotes/quotes_django/
quotes_django/wsgi.py", line 14, in <module>
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]     application = get_wsgi_application()
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]   File "/opt/rh/python27/root/usr/lib64/python2.7/site-
packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]     django.setup()
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]   File "/opt/rh/python27/root/usr/lib64/python2.7/site-
packages/django/__init__.py", line 21, in setup
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]     apps.populate(settings.INSTALLED_APPS)
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]   File "/opt/rh/python27/root/usr/lib64/python2.7/site-
packages/django/apps/registry.py", line 115, in populate
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]     app_config.ready()
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]   File "/opt/rh/python27/root/usr/lib64/python2.7/site-
packages/django/contrib/admin/apps.py", line 22, in ready
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]     self.module.autodiscover()
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]   File "/opt/rh/python27/root/usr/lib64/python2.7/site-
packages/django/contrib/admin/__init__.py", line 23, in autodiscover
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]     autodiscover_modules('admin', register_to=site)
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]   File "/opt/rh/python27/root/usr/lib64/python2.7/site-
packages/django/utils/module_loading.py", line 74, in autodiscover_modules
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]     import_module('%s.%s' % (app_config.name,         
module_to_search))
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]   File "/usr/lib64/python2.7/importlib/__init__.py", line 
37, in import_module
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]     __import__(name)
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]   File "/extra/www/html/quotes/quotes_django/quotespage/
admin.py", line 25
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]     approve_quotes.short_description = "Approve selected
quotes"
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]                  ^
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] SyntaxError: invalid syntax
[Sun Nov 23 13:53:36 2014] [info] [client 128.84.33.19] mod_wsgi (pid=19093, process='quotes.cs.cornell.edu',
  application='quotes.cs.cornell.edu|'): Loading WSGI script '/extra/www/html/quotes/quotes_django/quotes_django/
wsgi.py'.
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19] mod_wsgi (pid=19093): Target WSGI script '/extra/www/html/
quotes/quotes_django/quotes_django/wsgi.py' cannot be loaded as Python module.
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19] mod_wsgi (pid=19093): Exception occurred processing WSGI
script '/extra/www/html/quotes/quotes_django/quotes_django/wsgi.py'.
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19] Traceback (most recent call last):
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19]   File "/extra/www/html/quotes/quotes_django/         
quotes_django/wsgi.py", line 14, in <module>
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19]     application = get_wsgi_application()
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19]   File "/opt/rh/python27/root/usr/lib64/python2.7/site-
packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19]     django.setup()
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19]   File "/opt/rh/python27/root/usr/lib64/python2.7/site-
packages/django/__init__.py", line 21, in setup
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19]     apps.populate(settings.INSTALLED_APPS)
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19]   File "/opt/rh/python27/root/usr/lib64/python2.7/site-
packages/django/apps/registry.py", line 78, in populate
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19]     raise RuntimeError("populate() isn't reentrant")
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19] RuntimeError: populate() isn't reentrant

第一系列错误显示WSGI因my语法错误而失败admin.py。但是,第二系列错误似乎显示了Django内部的错误:

RuntimeError: populate() isn't reentrant

populate方法抛出registry.py

谷歌搜索此错误消息返回的信息少得令人惊讶,Django文档中没有这些信息。显然,如果你在中两次命名应用程序,有时可能会发生这种情况settings.py,但我没有这么做。更重要的是,settings.py自网站运行正常以来,我一直没有改变-我唯一改变的是admin.py

我尝试还原所做的所有更改,因此我所有的Python代码都恢复到网站正常运行的状态- populate() isn't reentrant当我尝试使WSGI重新加载代码时,仍然出现错误!

我还尝试在的INSTALLED_APPS部分中注释掉不同的应用程序settings.py,即使仅启用了django.contrib.staticfiles,该错误仍然会发生。奇怪的是,即使我注释掉所有应用程序,我仍然会收到错误消息-Django 即使未加载任何应用程序也会抛出错误!

有人知道这是怎么回事吗?还是因为Apache日志中的回溯对你没有任何帮助,所以我可以用哪种更好的方法来调试此错误?

注意:我正在使用Django 1.7,Apache 2.2和Python 2.7。


阅读 944

收藏
2020-03-26

共1个答案

小编典典

我的服务器的管理员重新启动了Apache,这神奇地解决了这个问题。完全相同的Python文件加载而不会引起populate() isn't reentrant。我什至尝试加载语法错误的另一个文件,然后对其进行修复,服务器能够加载新文件并正常运行而没有任何问题。

我仍然不知道出了什么问题,但是由于问题已经解决,因此我将其标记为“已解决”。(好吧,只要StackOverflow允许我接受自己的答案,我就会将其标记为已回答。)

更新:当我意外上传带有语法错误的Python并继续出现此错误后,我想出了一种解决方法,比重新启动Apache更容易。当WSGI开始引发populate() isn't reentrant错误时,我wsgi.py用以下简单函数替换了Django项目:

def application(environ, start_response):
    if environ['mod_wsgi.process_group'] != '': 
        import signal
        os.kill(os.getpid(), signal.SIGINT)
    return ["killed"]

然后,我重新加载我的网站,并且WSGI守护进程重新启动(即使网站仍然显示相同的500错误,我也可以通过查看Apache日志来判断)。

如果我然后wsgi.py恢复到正常状态并再次重新加载,则WSGI成功地拾取了我的代码而没有抛出populate() isn't reentrant错误(假设这次我没有语法错误)。因此,整个Apache不需要重新启动,仅需要重新启动WSGI进程,而我无需root特权就可以重新启动。

2020-03-26