简体   繁体   中英

Issue Running gunicorn to serve flask app

objective: get gunicorn to serve the flask app

attempt: run the following command:

gunicorn [the name of my project folder]:flaskr

output:

[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.

I have deployed this project to my web server. In the home directory of the account I am using to administer this webserver, I have the project folder as I was working on it from my workstation. Here is the tree output ran from within the project folder:

├── 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

I have installed gunicorn in the venv. I also exported the environment variables as shown in the flask tutorial.

I have also configured manifest.ini and setup.py to make the program installable. I have attempted to install by running the following from within the project folder:

pip install -e

I get the following output:

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

I have verified that the app itself is running on my workstation after using "flask run".

If you need code for any of the files, please let me know and the best way to share. Here is some code that I think it relevant for now:

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

EDIT: 8/26/19 - 1:30 pm:

I have attempted to discover the cause of this issue by following another article, https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-14-04 When I run this command:

gunicorn --bind 0.0.0.0:8000 wsgi

output:

[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.

Here is the wsgi.py:

from [the name of my project folder] import app

if __name__ == "__main__":
    app.run()

I suspect I am naming the wrong thing in my wsgi.py

Try inserting your project's paths to sys.path before attempting to import your application instance.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM