python - Amazon + Django 每12小时出现[Errno 5] Input/output error

标签 python django input amazon-ec2 output

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

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

errno 5 input/output error django

enter image description here

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

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 的基于类的 View 中

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

我搜索了这个错误,发现这张票报告了 https://code.djangoproject.com/ticket/23284

这个报告和我的错误非常相似......

另外我昨天也有这个错误,我重启了我的服务器,今天我又出现了这个错误。

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

我不认为我的应用程序的函数 get_context_data() 是问题...

最佳答案

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

我有两个新手错误:

  1. 打印 生产中的语句

在我上面问题中显示的回溯中,我的 get_context_data() 函数中有一个 print 语句:

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

有可能每次执行此打印语句时,进程都会尝试写入我的 amazon 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。

我一直在探索,一种可能性是根据运行我的应用程序的主机名称更改它们的值(真或假),such as shown in this post

在我的例子中,这是我的主机名的值

(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 用于我的测试环境。 我有一些 Hook ,它们在虚拟环境被激活时被激活

$VIRTUAL_ENV/bin/postactivatenrb_dev 中,我有这个:

export DJANGO_SETTINGS_MODULE="neurorehabilitation.settings.development"

以同样的方式,在 $VIRTUAL_ENV/bin/postactivatenrb_test 中,我有这个:

export DJANGO_SETTINGS_MODULE="neurorehabilitation.settings.testing"

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

export DJANGO_SETTINGS_MODULE="neurorehabilitation.settings.production"

只是为了测试效果和临时方式,我在 settings/production.py 中打印了 DEBUG

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 Logging functionality用于注册事件和我的应用程序的其他内容。

我应该探索 supervisor service用于管理生产中更好方式的 gunicorn yield 。

主管的其他资源:

How to install and manage supervisor in Ubuntu

Setting up Django with Nginx, Gunicorn, virtualenv, supervisor and PostgreSQL

关于python - Amazon + Django 每12小时出现[Errno 5] Input/output error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34643170/

相关文章:

python - urlopen windows ftp 错误

python - azure 管道中的 pip install azure-functions 因 pip 验证任务而失败

python - Django 在 DateField 的 auto_now_add 中使用哪个时区?

internet-explorer - 焦点事件触发 IE 与现代浏览器中的输入事件

java - 首先将第一个输入保存在文件夹中,然后引用它

python - Dnspython 无法解决任何问题

django - 反向未找到 : Sending Request Context in from templates

python - 在 Django 的 get_list_or_404 中排序

c - C中输入 double float

python - 无法使用 CNN 训练手势 (ASL) 模型