我有一个Django应用程序与nginx
+ gunicorn
一起提供3个gunicorn
工作进程。偶尔(可能每100个请求一次)其中一个工作进程进入一个状态,它开始使它服务的大多数(但不是全部)请求失败,然后当它试图向我发送电子邮件时它会引发异常。 gunicorn
错误日志如下所示:
[2015-04-29 10:41:39 +0000] [20833] [ERROR] Error handling request
Traceback (most recent call last):
File "/home/django/virtualenvs/homestead_django/local/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 130, in handle
File "/home/django/virtualenvs/homestead_django/local/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 171, in handle_request
File "/home/django/virtualenvs/homestead_django/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 206, in __call__
File "/home/django/virtualenvs/homestead_django/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 196, in get_response
File "/home/django/virtualenvs/homestead_django/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 226, in handle_uncaught_exception
File "/usr/lib/python2.7/logging/__init__.py", line 1178, in error
File "/usr/lib/python2.7/logging/__init__.py", line 1271, in _log
File "/usr/lib/python2.7/logging/__init__.py", line 1281, in handle
File "/usr/lib/python2.7/logging/__init__.py", line 1321, in callHandlers
File "/usr/lib/python2.7/logging/__init__.py", line 749, in handle
File "/home/django/virtualenvs/homestead_django/local/lib/python2.7/site-packages/django/utils/log.py", line 122, in emit
File "/home/django/virtualenvs/homestead_django/local/lib/python2.7/site-packages/django/utils/log.py", line 125, in connection
File "/home/django/virtualenvs/homestead_django/local/lib/python2.7/site-packages/django/core/mail/__init__.py", line 29, in get_connection
File "/home/django/virtualenvs/homestead_django/local/lib/python2.7/site-packages/django/utils/module_loading.py", line 26, in import_by_path
File "/home/django/virtualenvs/homestead_django/local/lib/python2.7/site-packages/django/utils/module_loading.py", line 21, in import_by_path
File "/home/django/virtualenvs/homestead_django/local/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
ImproperlyConfigured: Error importing module django.core.mail.backends.smtp: "No module named smtp"
因此,一些未被捕获的异常正在发生,然后Django试图通过电子邮件向我发送电子邮件。它无法导入django.core.mail.backends.smtp
的事实没有意义,因为django.core.mail.backends.smtp
肯定应该在工作进程的Python路径上。我可以从manage.py shell
导入它很好,我收到其他服务器错误(实际的软件错误)的电子邮件,所以我知道这很有用。这就像工作进程的环境以某种方式被破坏了。
一旦工人进程进入这种状态,它就很难恢复;几乎所有服务请求都以同样的方式失败。如果我重新启动gunicorn
一切都很好(直到另一个工作进程再次陷入这种奇怪的状态)。
我没有注意到任何明显的模式,所以我不认为这是由我的应用程序中的错误引发的(URL错误的出现是不同的,等等)。这似乎是某种竞争条件。
目前我正在使用gunicorn
的{{1}}选项来缓解此问题,但我想了解这里发生了什么。这是竞争条件吗?我该怎么调试呢?