uWSGI 无法使用 Flask 和 Virtualenv 找到“应用程序”

sky*_*ler 7 python wsgi uwsgi

使用 uWSGI 来提供一个简单的 wsgi 应用程序(一个简单的“Hello, World”)我的配置有效,但是当我尝试运行一个 Flask 应用程序时,我在 uWSGI 的错误日志中得到了这个:

current working directory: /opt/python-env/coefficient/lib/python2.6/site-packages
writing pidfile to /var/run/uwsgi.pid
detected binary path: /opt/uwsgi/uwsgi
setuid() to 497
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
uwsgi socket 0 bound to TCP address 127.0.0.1:3031 fd 3
Python version: 2.6.6 (r266:84292, Jun 18 2012, 14:18:47)  [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)]
Set PythonHome to /opt/python-env/coefficient/
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0xbed3b0
your server socket listen backlog is limited to 100 connections
*** Operational MODE: single process ***
added /opt/python-env/coefficient/lib/python2.6/site-packages/ to pythonpath.
unable to find "application" callable in file /var/www/coefficient/flask.py
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***`
Run Code Online (Sandbox Code Playgroud)

特别注意日志的这一部分:

无法在文件 /var/www/coefficient/flask.py 中找到可调用的“应用程序”

无法加载应用程序 0 (mountpoint='')(找不到可调用或导入错误)

****** 没有加载应用程序。进入全动态模式******

这是我的 Flask 应用程序:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello, World, from Flask!"
Run Code Online (Sandbox Code Playgroud)

在我将 Virtualenv 的 pythonpath 添加到我的配置文件之前,我收到了 Flask 的 ImportError。我解决了这个问题,我相信(我不再收到关于它的错误),这是我的完整配置文件:

uwsgi:
  #socket: /tmp/uwsgi.sock 
  socket: 127.0.0.1:3031
  daemonize: /var/log/uwsgi.log
  pidfile: /var/run/uwsgi.pid
  master: true
  vacuum: true
  #wsgi-file: /var/www/coefficient/coefficient.py
  wsgi-file: /var/www/coefficient/flask.py
  processes: 1
  virtualenv: /opt/python-env/coefficient/
  pythonpath: /opt/python-env/coefficient/lib/python2.6/site-packages
Run Code Online (Sandbox Code Playgroud)

这就是我从 rc 脚本启动 uWSGI 的方式:

/opt/uwsgi/uwsgi --yaml /etc/uwsgi/conf.yaml --uid uwsgi

如果我尝试在浏览器中查看 Flask 程序,我会得到以下信息:

**uWSGI Error**

Python application not found
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏。

小智 14

无法在文件 /var/www/coefficient/flask.py 中找到可调用的“应用程序”

是关键:)

您的应用程序正在定义一个可调用的“应用程序”,因此您必须指示 uWSGI 搜索它,而不是“应用程序”。

您可以使用该选项

callable: app

它会起作用(这在官方 Flask 文档中有解释)


toz*_*CSS 5

或者,您可以添加module = flaskapp:app到您的 ini。

此外,确实,callableuwsgi-docs 中更清楚地解决了:

Flask 将其 WSGI 函数(我们在本快速入门开头称为“应用程序”的函数)导出为“app”,因此我们需要指示 uWSGI 使用它: uwsgi --wsgi-file myflaskapp.py --callable app