使用postgresql数据库时uwsgi下的django应用程序无法启动

Posted

技术标签:

【中文标题】使用postgresql数据库时uwsgi下的django应用程序无法启动【英文标题】:django app under uwsgi failing to start when using postgresql database 【发布时间】:2016-04-23 06:07:26 【问题描述】:

我在 Ubuntu 14.04 on AWS -> NGINX -> UWSGI -> DJANGO APP 中设置了一个应用程序。 该应用程序在使用 sqllite3 时运行完全正常,但是当我使用 django.db.backends.postgresql_psycopg2w 作为我的数据库引擎时,它无法加载,我在 uwsgi 日志中发现了这一点:

- *** Starting uWSGI 2.0.12 (64bit) on [Mon Jan 18 05:36:25 2016] ***
 - compiled with version: 4.8.4 on 18 January 2016 04:10:30
 - os: Linux-3.13.0-48-generic #80-Ubuntu SMP Thu Mar 12 11:16:15 UTC 2015
 - nodename: ip-172-31-55-58
 - machine: x86_64
 - clock source: unix
 - detected number of CPU cores: 1
 - current working directory: /var/www/carzumer_api
 - detected binary path: /usr/local/bin/uwsgi
 - !!! no internal routing support, rebuild with pcre support !!!
 - setgid() to 33
 - setuid() to 33
 - chdir() to /var/www/carzumer_api/src/
 - your processes number limit is 7862
 - limiting address space of processes...
 - your process address space limit is 134217728 bytes (128 MB)
 - your memory page size is 4096 bytes
 -  *** WARNING: you have enabled harakiri without post buffering. Slow upload could be rejected on post-unbuffered webservers ***
 - detected max file descriptor number: 1024
 - lock engine: pthread robust mutexes
 - thunder lock: disabled (you can enable it with --thunder-lock)
 - uwsgi socket 0 bound to UNIX address /var/www/carzumer_api/run/uwsgi.sock fd 6
 - Python version: 2.7.6 (default, Jun 22 2015, 18:01:27)  [GCC 4.8.2]
 - Set PythonHome to /var/www/carzumer_api/env
 - *** Python threads support is disabled. You can enable it with --enable-threads ***
 - Python main interpreter initialized at 0x2047200
 - your server socket listen backlog is limited to 100 connections
 - your mercy for graceful operations on workers is 60 seconds
 - mapped 363840 bytes (355 KB) for 4 cores
 - *** Operational MODE: preforking ***
Traceback (most recent call last):
  File "./carzumer_api/wsgi.py", line 16, in <module>
    application = get_wsgi_application()
  File "/var/www/carzumer_api/env/local/lib/python2.7/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
    django.setup()
  File "/var/www/carzumer_api/env/local/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/var/www/carzumer_api/env/local/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/var/www/carzumer_api/env/local/lib/python2.7/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/var/www/carzumer_api/env/local/lib/python2.7/site-packages/django/contrib/auth/models.py", line 41, in <module>
    class Permission(models.Model):
  File "/var/www/carzumer_api/env/local/lib/python2.7/site-packages/django/db/models/base.py", line 139, in __new__
    new_class.add_to_class('_meta', Options(meta, **kwargs))
  File "/var/www/carzumer_api/env/local/lib/python2.7/site-packages/django/db/models/base.py", line 324, in add_to_class
    value.contribute_to_class(cls, name)
  File "/var/www/carzumer_api/env/local/lib/python2.7/site-packages/django/db/models/options.py", line 250, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/var/www/carzumer_api/env/local/lib/python2.7/site-packages/django/db/__init__.py", line 36, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/var/www/carzumer_api/env/local/lib/python2.7/site-packages/django/db/utils.py", line 240, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/var/www/carzumer_api/env/local/lib/python2.7/site-packages/django/db/utils.py", line 111, in load_backend
    return import_module('%s.base' % backend_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/var/www/carzumer_api/env/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 24, in <module>
    raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: libgpg-error.so.0: failed to map segment from shared object: Cannot allocate memory
 - unable to load app 0 (mountpoint='') (callable not found or import error)
 - *** no app loaded. going in full dynamic mode ***
 - *** uWSGI is running in multiple interpreter mode ***
 - spawned uWSGI master process (pid: 11351)
 - spawned uWSGI worker 1 (pid: 11354, cores: 1)
 - spawned uWSGI worker 2 (pid: 11355, cores: 1)
 - --- no python application found, check your startup logs for errors ---
address space usage: 81760256 bytes/77MB rss usage: 21651456 bytes/20MB [pid: 11355|app: -1|req: -1/1] 127.0.0.1 () 32 vars in 351 bytes [Mon Jan 18 05:37:50 2016] GET / => generated 21 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 82 bytes (0 switches on core 0)

我已经使用 apt-get 安装了以下软件包: libpq-dev python-dev postgresql postgresql-contrib nginx python-pip

psycopg2==2.6.1 包也安装在我的 venv 中。

谁能指出我正确的方向,我已经搜索了几个小时,不知道为什么会失败。

【问题讨论】:

【参考方案1】:

我终于弄清楚了我的问题所在。在我的 uwsgi.ini 文件中,我有

limit-as = 128

这个开关将uwsgi进程的地址空间限制为128mb。我想这对于 PostgreSQL 驱动程序来说还不够。卸下开关后,一切都像魅力一样。 尽管最好找到一个有效的值,而不是完全删除该值。

【讨论】:

我认为您可以将其标记为答案,不是吗?我有同样的问题,这也解决了它。也就是说,将其设置为有效的东西而不是完全删除它可能是更好的做法。 我同意,找到一个有效的值肯定比仅仅删除更好。 +1 来自我 ;) 真的,谢谢你。花了一个小时试图弄清楚。谢谢@gregor2004ua,谢谢 ***! 我在这方面浪费了大约 12 个小时。非常感谢@gregor2004ua【参考方案2】:

根异常是这样的:

django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: libgpg-error.so.0: failed to map segment from shared object: Cannot allocate memory

根据我的发现,可能是 32/64 位问题。例如,看这个页面:

https://serverfault.com/questions/329418/cannot-allocate-memory-error-for-psycopg2-and-libgcrypt-so-on-debian-6-64bit

您可能想要使用file 工具并检查您的库的架构(特别是_psycopg.solibgpg-error.so.0)。也可以和python -c "import platform; print platform.architecture()"比较

【讨论】:

以上是关于使用postgresql数据库时uwsgi下的django应用程序无法启动的主要内容,如果未能解决你的问题,请参考以下文章

Django部署uwsgi+nginx

django Nginx部署

Nginx+uWSGI+Django部署方案的详细实现过程

nginx+uwsgi+django的搭建笔记

为本地开发设置 django 和 dj-database-url

uWSGI+Nginx+Flask在Linux下的部署