[英]Issue Running gunicorn to serve flask app
目標:讓Gunicorn服務於燒瓶應用
嘗試:運行以下命令:
gunicorn [the name of my project folder]:flaskr
輸出:
[2019-08-26 15:04:32 +0000] [16063] [INFO] Starting gunicorn 19.9.0
[2019-08-26 15:04:32 +0000] [16063] [INFO] Listening at: http://127.0.0.1:8000 (16063)
[2019-08-26 15:04:32 +0000] [16063] [INFO] Using worker: sync
[2019-08-26 15:04:32 +0000] [16067] [INFO] Booting worker with pid: 16067
[2019-08-26 15:04:32 +0000] [16067] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/gunicorn/arbiter.py", line 583, in spawn_worker
worker.init_process()
File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/base.py", line 129, in init_process
self.load_wsgi()
File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/base.py", line 138, in load_wsgi
self.wsgi = self.app.wsgi()
File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 52, in load
return self.load_wsgiapp()
File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
return util.import_app(self.app_uri)
File "/usr/local/lib/python2.7/dist-packages/gunicorn/util.py", line 350, in import_app
__import__(module)
ImportError: No module named [the name of my project folder]
[2019-08-26 15:04:32 +0000] [16067] [INFO] Worker exiting (pid: 16067)
[2019-08-26 15:04:32 +0000] [16063] [INFO] Shutting down: Master
[2019-08-26 15:04:32 +0000] [16063] [INFO] Reason: Worker failed to boot.
我已經將該項目部署到了我的Web服務器。 在我用來管理該Web服務器的帳戶的主目錄中,有一個項目文件夾,因為我正在從工作站上對其進行處理。 這是從項目文件夾中運行的樹輸出:
├── flaskr
│ ├── auth.py
│ ├── build
│ ├── db.py
│ ├── db.pyc
│ ├── dist
│ ├── flaskr.egg-info
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── MANIFEST.in
│ ├── old
│ ├── __pycache__
│ ├── schema.sql
│ ├── setup.py
│ ├── static
│ └── templates
├── instance
│ └── flaskr.sqlite
├── keys.csv
├── tests
├── venv
│ ├── bin
│ ├── include
│ ├── lib
│ ├── lib64
│ ├── pyvenv.cfg
│ └── share
└── venv.sh
我已將Venicorn安裝在靜脈內。 我還導出了燒瓶教程中所示的環境變量。
我還配置了manifest.ini和setup.py以使程序可安裝。 我試圖通過在項目文件夾中運行以下命令進行安裝:
pip install -e
我得到以下輸出:
Usage:
pip install [options] <requirement specifier> [package-index-options] ...
pip install [options] -r <requirements file> [package-index-options] ...
pip install [options] [-e] <vcs project url> ...
pip install [options] [-e] <local project path> ...
pip install [options] <archive url/path> ...
-e option requires an argument
我已驗證使用“ flask run”后,應用程序本身已在我的工作站上運行。
如果您需要任何文件的代碼,請告訴我以及最佳共享方式。 這是一些我認為目前相關的代碼:
setup.py:
from setuptools import find_packages, setup
setup(
name='flaskr',
version='1.0.0',
packages=find_packages(),
include_package_data=True,
zip_safe=False,
install_requires=[
'flask',
],
)
manifest.in:
include flaskr/schema.sql
graft flaskr/static
graft flaskr/templates
global-exclude *.pyc
編輯:19/8/26-下午1:30:
我試圖通過關注另一篇文章https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu來發現此問題的原因-14-04當我運行此命令時:
gunicorn --bind 0.0.0.0:8000 wsgi
輸出:
[2019-08-26 17:23:14 +0000] [17748] [INFO] Starting gunicorn 19.9.0
[2019-08-26 17:23:14 +0000] [17748] [INFO] Listening at: http://0.0.0.0:8000 (17748)
[2019-08-26 17:23:14 +0000] [17748] [INFO] Using worker: sync
[2019-08-26 17:23:14 +0000] [17752] [INFO] Booting worker with pid: 17752
[2019-08-26 17:23:15 +0000] [17752] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/gunicorn/arbiter.py", line 583, in spawn_worker
worker.init_process()
File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/base.py", line 129, in init_process
self.load_wsgi()
File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/base.py", line 138, in load_wsgi
self.wsgi = self.app.wsgi()
File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 52, in load
return self.load_wsgiapp()
File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
return util.import_app(self.app_uri)
File "/usr/local/lib/python2.7/dist-packages/gunicorn/util.py", line 350, in import_app
__import__(module)
File "/home/[my user name]/[name of the project folder]/wsgi.py", line 1, in <module>
from [name of the project folder] import app
ImportError: No module named [name of the project folder]
[2019-08-26 17:23:15 +0000] [17752] [INFO] Worker exiting (pid: 17752)
[2019-08-26 17:23:15 +0000] [17748] [INFO] Shutting down: Master
[2019-08-26 17:23:15 +0000] [17748] [INFO] Reason: Worker failed to boot.
這是wsgi.py:
from [the name of my project folder] import app
if __name__ == "__main__":
app.run()
我懷疑我在wsgi.py中命名錯誤的內容
在嘗試導入應用程序實例之前,請嘗試將項目的路徑插入sys.path
。
wsgi.py:
import os
import sys
directory = os.path.dirname(os.path.realpath(__file__))
app_directory = os.path.join(directory, 'flaskr')
if directory not in sys.path:
sys.path.insert(0, directory)
if app_directory not in sys.path:
sys.path.insert(0, app_directory)
from flaskr import app
if __name__ == '__main__':
app.run()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.