Python - RuntimeError:populate()不可重入

时间:2017-08-01 11:28:49

标签: python django apache mod-wsgi wsgi

我有石墨服务器,间歇性地它的守护进程因此错误而失败,

HTTP CRITICAL: HTTP/1.1 500 Internal Server Error - string 'datapoints' not found on 'http://192.168.12.15:8000/render/?target=sys.example_com.snmp.if_octets-Gi0_0_0.rx&format=json&from=-5min' - 723 bytes in 0.002 second response time |time=0.002067s;;;0.000000 size=723B;;;0

所以我重新启动了修复问题的apache。但我想解决这个问题的根本原因,我不能每次都重启apache。我想永久地解决这个问题。这是我在错误日志中找到的,

[Tue Aug 01 20:16:01] [wsgi:error] RuntimeError: populate() isn't reentrant
[Tue Aug 01 20:16:21] [wsgi:error] Target WSGI script '/usr/lib/python2.7/dist-packages/graphite/wsgi.py' cannot be loaded as Python module.
[Tue Aug 01 20:16:21] [wsgi:error] Exception occurred processing WSGI script '/usr/lib/python2.7/dist-packages/graphite/wsgi.py'.
[Tue Aug 01 20:16:21] [wsgi:error] Traceback (most recent call last):
[Tue Aug 01 20:16:21] [wsgi:error] File "/usr/lib/python2.7/dist-packages/graphite/wsgi.py", line 14, in <module>
[Tue Aug 01 20:16:21] [wsgi:error] application = get_wsgi_application()
[Tue Aug 01 20:16:21] [wsgi:error] File "/usr/lib/python2.7/dist-packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Tue Aug 01 20:16:21] [wsgi:error] django.setup()
[Tue Aug 01 20:16:21] [wsgi:error] File "/usr/lib/python2.7/dist-packages/django/__init__.py", line 21, in setup
[Tue Aug 01 20:16:21] [wsgi:error] apps.populate(settings.INSTALLED_APPS)
[Tue Aug 01 20:16:21] [wsgi:error] File "/usr/lib/python2.7/dist-packages/django/apps/registry.py", line 78, in populate

这是我的/usr/lib/python2.7/dist-packages/graphite/wsgi.py文件,

import os
import sys

try:
    from importlib import import_module
except ImportError:
    from django.utils.importlib import import_module

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'graphite.settings')  # noqa

from django.conf import settings
from django.core.wsgi import get_wsgi_application

application = get_wsgi_application()

try:
    import whitenoise
except ImportError:
    whitenoise = False
else:
    # WhiteNoise < 2.0.1 does not support Python 2.6
    if sys.version_info[:2] < (2, 7):
        whitenoise_version = tuple(map(
                int, getattr(whitenoise, '__version__', '0').split('.')))
        if whitenoise_version < (2, 0, 1):
            whitenoise = False

if whitenoise:
    from whitenoise.django import DjangoWhiteNoise
    application = DjangoWhiteNoise(application)
    prefix = "/".join((settings.URL_PREFIX.strip('/'), 'static'))
    for directory in settings.STATICFILES_DIRS:
        application.add_files(directory, prefix=prefix)
    for app_path in settings.INSTALLED_APPS:
        module = import_module(app_path)
        directory = os.path.join(os.path.dirname(module.__file__), 'static')
        if os.path.isdir(directory):
            application.add_files(directory, prefix=prefix)

更新

这是我正在使用的石墨配置文件,

<VirtualHost *:8000>
  ServerName graphite-web

  ## Vhost docroot
  DocumentRoot "/var/www"
  ## Alias declarations for resources outside the DocumentRoot
  Alias /static "/var/lib/graphite/webapp/content"

  ## Directories, there should at least be a declaration for /var/www

  <Directory "/var/www">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Require all granted
  </Directory>

  ## Logging
  ErrorLog "/var/log/apache2/graphite-web_error.log"
  ServerSignature Off
  CustomLog "/var/log/apache2/graphite-web_access.log" combined
  WSGIScriptAlias / "/usr/lib/python2.7/dist-packages/graphite/wsgi.py"
</VirtualHost>

是什么导致了这个问题?对此有何帮助?

1 个答案:

答案 0 :(得分:1)

您需要返回并在此之前在日志中找到错误。也就是说,针对进程的第一个请求,或者在启用了预加载的情况下加载WSGI脚本的请求。你给出的错误不是根本原因,但后续请求的结果在第一次失败之后失败,因为Django初始化在第一次失败时不再是重入的。

如果是暂时性问题,您应该确保使用mod_wsgi守护程序模式并在startup-timeout=15上设置WSGIDaemonProcess,以便从瞬态问题中自动恢复。暂时失败的一个原因是Apache首次启动时数据库不可用。

您需要确保您拥有最近的mod_wsgi版本,而不是Linux发行版的古老版本才能获得startup-timeout选项。

我还建议你添加你正在使用的mod_wsgi配置的问题,这样可以评论你是否应该采取不同的其他方式。