Amazon + Django 每12小时出现[Errno 5] 输入/输出错误

2024-01-03

我最近设置并部署了一个 Amazon EC2 实例来部署我的 django 项目。

当我在浏览器中收到此错误时,我正在通过浏览器与应用程序进行交互:

errno 5 input/output error django

这个错误确实引用了我的应用程序的某些功能

Environment:

Request Method: GET
Request URL: http://localhost:8000/accounts/profile/

Django Version: 1.9
Python Version: 3.4.3
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'crispy_forms',
 'django_extensions',
 'storages',
 'userprofile']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/home/ubuntu/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "/home/ubuntu/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/ubuntu/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "/home/ubuntu/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/utils/decorators.py" in _wrapper
  67.             return bound_func(*args, **kwargs)

File "/home/ubuntu/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  23.                 return view_func(request, *args, **kwargs)

File "/home/ubuntu/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/utils/decorators.py" in bound_func
  63.                 return func.__get__(self, type(self))(*args2, **kwargs2)

File "/home/ubuntu/workspace/neurorehabilitation-system/userprofile/mixins.py" in dispatch
  7.         return super(LoginRequiredMixin, self).dispatch(request, *args, **kwargs)

File "/home/ubuntu/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/views/generic/base.py" in dispatch
  88.         return handler(request, *args, **kwargs)

File "/home/ubuntu/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/views/generic/base.py" in get
  157.         context = self.get_context_data(**kwargs)

File "/home/ubuntu/workspace/neurorehabilitation-system/userprofile/views.py" in get_context_data
  50.             print (user.is_physiotherapist)

Exception Type: OSError at /accounts/profile/
Exception Value: [Errno 5] Input/output error

在第 50 行的末尾引用了get_context_data()函数位于继承的基于类的视图内部TemplateView CBV

但在我的控制台中,服务器需要重新启动,当我这样做时,错误以一种神奇的方式解决了..

我搜索过此错误,发现已报告此票证https://code.djangoproject.com/ticket/23284 https://code.djangoproject.com/ticket/23284

该报告与我的错误非常相似......

另外我昨天遇到了这个错误,我重新启动了服务器,今天又出现了这个错误。

Django 的 EC2 基础设施存在一些问题(我不这么认为),或者问题更多地出在我的应用程序方面?

我不认为这个功能get_context_data()我的应用程序的问题是......


我一直在探索,应该说这个错误的根源在于我的代码中

我有两个新手错误:

  1. print 生产中的句子

在我上面的问题中显示的回溯中,我有一个print我里面的一句话get_context_data()这种方式的作用:

File "/home/ubuntu/workspace/neurorehabilitation-system/userprofile/views.py" in get_context_data
  50.             print (user.is_physiotherapist)

有可能每次执行此打印语句时,该进程都会尝试写入我的亚马逊 ec2 机器实例中的 stdout 文件。

我删除了该行中的这个打印句子,并通过 git 将更改检索到我的生产服务器中,然后重新启动 Gunicorn 服务器,一切工作都很完美。

  1. 我有 DEBUG=True 生产中

我有以下设置文件:

settings/
    base.py # --- without DEBUG
    development.py # --- DEBUG=True
    testing.py # --- DEBUG=True
    production.py # --- DEBUG=False
    staging.py # --- DEBUG=False  

全部文件 (development.py, testing.py, production.py, staging.py)继承自base.py

但我不知道如何在我的 ec2 实例中执行 production.py,它继承了所有的 base.py 并将 DEBUG 覆盖为 False。

我一直在探索,一种可能性是根据运行我的应用程序的主机的名称更改它们的值(True 或 False),如这篇文章所示 https://nicksergeant.com/automatically-setting-debug-in-your-django-app-based-on-server-hostname/

就我而言,这是我的主机名的值

(nrb_dev)ubuntu@ip-172-31-27-249:~$ python
Python 3.4.3 (default, Oct 14 2015, 20:28:29) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> a=socket.gethostname()
>>> a
'ip-172-31-27-249'
>>> 
>>> if a != 'ip-172-31-27-249':
...     DEBUG = print ('Caleno juiciocito')
... 
>>> DEBUG
True
>>> 

这意味着,将以下内容放入我的 base.py 中:

import socket

if socket.gethostname() == 'ip-172-31-27-249':
    DEBUG = False
else:
    DEBUG = True

尽管我在代码中硬编码了生产服务器的主机名。 这意味着当我们想要使用其他主机名在其他计算机上部署我的项目时,我将在手动修改后添加一个点

尽管它有效,但这是否是最佳实践?

我认为最合适的另一个选择是修复我的价值DJANGO_SETTINGS_MODULE环境变量

在我的特殊情况下我正在使用virtualenvwrapper我有两个虚拟环境,所以:

nrb_dev对于我的开发环境

nrb_test对于我的测试环境。 我有一些钩子在激活虚拟环境时被激活

In nrb_dev in $VIRTUAL_ENV/bin/postactivate我有这个:

export DJANGO_SETTINGS_MODULE="neurorehabilitation.settings.development"

以同样的方式,在nrb_test in $VIRTUAL_ENV/bin/postactivate我有这个:

export DJANGO_SETTINGS_MODULE="neurorehabilitation.settings.testing"

这意味着在我的亚马逊 EC2 生产机器中我应该更改钩子$VIRTUAL_ENV/bin/postactivate来选择settings/production.py这样的方式:

export DJANGO_SETTINGS_MODULE="neurorehabilitation.settings.production"

只是为了测试效果和时间方式,我打印了DEBUG我的价值settings/production.py

from .base import *

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
print (DEBUG) # just for now.

当我启动 Gunicorn 守护进程服务器时,我可以看到 DEBUG 值设置为False

(nrb_dev)ubuntu@ip-172-31-27-249:~/workspace/neurorehabilitation-system$ gunicorn -c neurorehabilitation/gunicorn_config.py neurorehabilitation.wsgi 
[2016-01-08 00:26:15 +0000] [6691] [INFO] Starting gunicorn 19.4.5
[2016-01-08 00:26:15 +0000] [6691] [INFO] Listening at: http://127.0.0.1:8000 (6691)
[2016-01-08 00:26:15 +0000] [6691] [INFO] Using worker: sync
[2016-01-08 00:26:15 +0000] [6694] [INFO] Booting worker with pid: 6694
False
^C[2016-01-08 00:26:19 +0000] [6691] [INFO] Handling signal: int

补充笔记

我可以探索 Django记录功能 https://docs.djangoproject.com/en/1.9/topics/logging/用于注册事件和我的应用程序的其他内容。

我应该探索督导服务 http://docs.gunicorn.org/en/latest/deploy.html#supervisor以更好的方式管理生产中的gunicorn进程。

主管的其他资源:

如何在Ubuntu中安装和管理supervisor https://www.digitalocean.com/community/tutorials/how-to-install-and-manage-supervisor-on-ubuntu-and-debian-vps

使用 Nginx、Gunicorn、virtualenv、supervisor 和 PostgreSQL 设置 Django http://michal.karzynski.pl/blog/2013/06/09/django-nginx-gunicorn-virtualenv-supervisor/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Amazon + Django 每12小时出现[Errno 5] 输入/输出错误 的相关文章

随机推荐