目标:让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
我已经在Venv中安装了gunicorn。我还导出了烧瓶教程中所示的环境变量。
我还配置了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中命名错误的内容
答案 0 :(得分:-1)
在尝试导入应用程序实例之前,尝试将项目的路径插入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()