Django 停止使用 RuntimeError:populate() 不可重入 [英] Django stops working with RuntimeError: populate() isn't reentrant

查看:33
本文介绍了Django 停止使用 RuntimeError:populate() 不可重入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been developing a Django web application deployed on an Apache server with WSGI, and everything has been going smoothly. Today, I made some minor changes to my app's admin.py in an attempt to customize the build-in Django Admin interface, and initially made a syntax error (an unclosed parenthesis). This meant that when I touched wsgi.py and loaded the code (I have WSGI running in daemon mode on my virtual host), my website was replaced with an Internal Server Error because WSGI stopped when it hit the syntax error.

所以我修复了语法错误,检查我没有更多的manage.py check,然后触摸wsgi.py 重新部署.但是我的网站仍然显示内部服务器错误!检查 Apache 日志,这是我看到的:

So I fixed the syntax error, checked that I didn't have any more with manage.py check, and touched wsgi.py to redeploy. But my website still displays an Internal Server Error! Checking the Apache logs, this is what I see:

[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 由于我的 admin.py 中的语法错误而失败.但是,第二系列错误似乎显示了 Django 内部的错误:

The first series of errors shows WSGI failing due to the syntax error in my admin.py. However, the second series of errors seems to show an error internal to Django:

RuntimeError: populate() isn't reentrant

registry.pypopulate 方法抛出.

thrown from the populate method of registry.py.

谷歌搜索此错误消息返回的信息非常少,没有来自 Django 文档的信息.显然,如果您在 settings.py 中两次命名一个应用程序,有时会发生这种情况,但我没有这样做.更重要的是,自从网站运行良好以来,我没有更改 settings.py —— 我唯一改变的是 admin.py.

Googling this error message returns surprisingly little information, none of it from Django documentation. Apparently, it can sometimes happen if you name an app twice in your settings.py, but I'm not doing that. More importantly, I haven't changed settings.py since the point where the website was working fine -- the only thing I changed was admin.py.

我尝试恢复我所做的所有更改,所以我所有的 Python 代码都回到了网站工作时的状态——但我仍然得到 populate() 不可重入当我尝试让 WSGI 重新加载代码时出错!

I tried reverting all the changes I made, so all my Python code is back in the state it was when the website was working -- and I still get the populate() isn't reentrant error when I try to make WSGI reload the code!

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

I've also tried commenting-out different apps in the INSTALLED_APPS section of settings.py, and even with only 'django.contrib.staticfiles' enabled the error still happens. Weirdly, I still get the error even if I comment out all the apps -- Django throws the error even when it isn't loading any apps!

有人知道这是怎么回事吗?或者我有什么更好的方法来调试这个错误,因为 Apache 日志中的回溯非常无用?

Does anyone know what's going on here? Or any better way for me to debug this error, since the traceback in the Apache log is pretty unhelpful?

注意:我使用的是 Django 1.7、Apache 2.2 和 Python 2.7.

Notes: I'm using Django 1.7, Apache 2.2, and Python 2.7.

推荐答案

这是由 Django 设置中某处的错误引起的.不幸的是,Django 将错误隐藏在这个通用且无用的错误消息后面.

This is caused by a bug in your Django settings somewhere. Unfortunately, Django's hiding the bug behind this generic and un-useful error message.

要揭示真正的问题,请打开 django/apps/registry.py 并在第 80 行附近替换:

To reveal the true problem, open django/apps/registry.py and around line 80, replace:

raise RuntimeError("populate() isn't reentrant")

与:

self.app_configs = {}

这将允许 Django 继续加载,并显示实际错误.

This will allow Django to continue loading, and reveal the actual error.

由于多种不同的原因,我遇到了此错误.曾经是因为我在我的一个应用程序的 admin.py 中有一个错误的导入.

I've encountered this error for several different causes. Once was because I had a bad import in one of my app's admin.py.

这篇关于Django 停止使用 RuntimeError:populate() 不可重入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆