Ubuntu

gunicorn 停止查找 wsgi 目錄

  • November 7, 2020

我用 nginx gunicorn 和 supervisor 配置了一個 django 應用程序。幾個月來一切都執行良好,直到今天。我沒有觸及任何配置文件,但不知何故,502 bad gateway當我嘗試訪問我網站的任何頁面時,我現在得到了響應。

Gunicorn 在這裡有問題,這裡是它正在輸出的日誌:

Traceback (most recent call last):
 File "/home/ubuntu/exo/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
   worker.init_process()
 File "/home/ubuntu/exo/lib/python3.6/site-packages/gunicorn/workers/ggevent.py", line 162, in init_process
   super().init_process()
 File "/home/ubuntu/exo/lib/python3.6/site-packages/gunicorn/workers/base.py", line 119, in init_process
   self.load_wsgi()
 File "/home/ubuntu/exo/lib/python3.6/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
   self.wsgi = self.app.wsgi()
 File "/home/ubuntu/exo/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
   self.callable = self.load()
 File "/home/ubuntu/exo/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
   return self.load_wsgiapp()
 File "/home/ubuntu/exo/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
   return util.import_app(self.app_uri)
 File "/home/ubuntu/exo/lib/python3.6/site-packages/gunicorn/util.py", line 358, in import_app
   mod = importlib.import_module(module)
 File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
   return _bootstrap._gcd_import(name[level:], package, level)
 File "<frozen importlib._bootstrap>", line 994, in _gcd_import
 File "<frozen importlib._bootstrap>", line 971, in _find_and_load
 File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
 File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
 File "<frozen importlib._bootstrap>", line 994, in _gcd_import
 File "<frozen importlib._bootstrap>", line 971, in _find_and_load
 File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'inventory4'

我非常困惑,因為之前一切都執行良好!

這是我對 gunicorn 的 django 配置:

settings.py:

WSGI_APPLICATION = 'inventory4.wsgi.application'

在 wsgi.py 中:

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'inventory4.settings')

application = get_wsgi_application()

這就是我的 /etc/supervisor/conf.d/gunicorn.conf

directory=/home/ubuntu/exostocksaas
command=/home/ubuntu/exo/bin/gunicorn  --config /home/ubuntu/exo/lib/python3.6/site-packages/gunicorn/config.py --log-level debug --bind unix:/home/ubuntu/exostocksaas/app.sock inventory4.wsgi:application

autostart=true
autorestart=true
stderr_logfile=/var/log/gunicorn/gunicorn.err.log
stdout_logfile=/var/log/gunicorn/gunicorn.out.log


[group:guni]
programs:gunicorn

這是我在 gunicorn 包中的 config.py 文件:

BASE_DIR = "/home/ubuntu/"
sys.path.append(BASE_DIR)


bind = '127.0.0.1:8000'
backlog = 2048


import multiprocessing
daemon: True
workers = 20
worker_class = 'gevent'
worker_connections = 1000
timeout = 300
graceful_timeout = 300
keepalive = 300
inactivity_timeout=300
errorlog = '-'
loglevel = 'info'
accesslog = '-'
threads = 2

老實說,我剛剛對 django 應用程序進行了更新,所以這很奇怪,有人知道可能導致此問題的事情的線索或經驗嗎?

我現在正在考慮的事情可能是我的虛擬機出現負載問題,這會導致 gunicorn 崩潰嗎?這是我跑步時注意到的landscape-sysinfo

System load:  9.56              Processes:           135
 Usage of /:   80.7% of 7.69GB   Users logged in:     1
 Memory usage: 38%               IP address for eth0: xxx.xx.xx.xx
 Swap usage:   0%

這可能是問題嗎?

我找到了這個問題的解決方案,以防有人絆倒她,免責聲明^^我無法解釋為什麼它停止工作以及為什麼解決方案解決了這個問題。gunicorn 日誌中顯示的錯誤似乎是一種預設問題,真正的問題是無聲的。我懷疑是CPU問題。

我只是去了 /venv/lib/python3.6/site-packages/gunicorn/config.conf 並添加

threads = 2

它以前設置為 1,然後設置為 2 就可以了。我非常願意接受任何更好的解決方案,或者解釋它解決問題的原因。

引用自:https://serverfault.com/questions/1041551