【问题标题】:Flask with UWSGI: Python application not found带有 UWSGI 的烧瓶:找不到 Python 应用程序
【发布时间】:2014-11-17 23:35:25
【问题描述】:

我已按照Linode guide 使用 Flask 应用程序设置 UWSGI,在我尝试使用我的应用程序而不是示例应用程序之前,一切似乎都正常,当我收到“未找到 uWSGI 错误 Python 应用程序”时。

我在/srv/www/xxx.com/xxx/中的文件结构是:

app/
    __init__.py
uwsgi.py

__init__.py 文件包含运行应用程序的行:

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

uwsgi.py 导入应用程序:

#!flask/bin/python

from app import app

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

当我手动尝试运行python uwsgi.py 时,该应用程序似乎可以运行,因此没有错误或错误的导入。如果我将uwsgi.py 的内容更改为这样的内容(来自示例):

import os
import sys

sys.path.append('/srv/www/example.com/application')

os.environ['PYTHON_EGG_CACHE'] = '/srv/www/example.com/.python-egg'

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                    ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

应用程序似乎在运行,因此 UWSGI 和 nginx 工作正常并运行 uwsgi.py 文件,但当我将其指向我的应用程序时却没有。

我的 uwsgi 配置:

<uwsgi>
    <plugin>python</plugin>
    <socket>/run/uwsgi/app/xxx.com/xxx.com.socket</socket>
    <pythonpath>/srv/www/xxx.com/application/xxx/</pythonpath>
    <app mountpoint="/">

        <script>uwsgi</script>

    </app>
    <master/>
    <processes>4</processes>
    <harakiri>60</harakiri>
    <reload-mercy>8</reload-mercy>
    <cpu-affinity>1</cpu-affinity>
    <stats>/tmp/stats.socket</stats>
    <max-requests>2000</max-requests>
    <limit-as>512</limit-as>
    <reload-on-as>256</reload-on-as>
    <reload-on-rss>192</reload-on-rss>
    <no-orphans/>
    <vacuum/>
</uwsgi>

我的 nginx 配置:

server {
        listen          80;
        server_name     $hostname;
        access_log /srv/www/xxx.com/logs/access.log;
        error_log /srv/www/xxx.com/logs/error.log;

        location / {
            uwsgi_pass      unix:///run/uwsgi/app/xxx.com/xxx.com.socket;
            include         uwsgi_params;
            uwsgi_param     UWSGI_SCHEME $scheme;
            uwsgi_param     SERVER_SOFTWARE    nginx/$nginx_version;

        }

        location /static {
            root   /srv/www/xxx.com/public_html/static/;
            index  index.html index.htm;

        }

}

这是 uwsgi 错误日志:

added /srv/www/xxx.com/application/xxx/ to pythonpath.
- 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 ***
- spawned uWSGI master process (pid: 21982)
- spawned uWSGI worker 1 (pid: 21990, cores: 1)
- set cpu affinity for worker 1 toTue Sep 23 16:51:19 2014 -  0Tue Sep 23 16:51:19 2014 -
- spawned uWSGI worker 2 (pid: 21991, cores: 1)
- *** Stats server enabled on /tmp/stats.socket fd: 14 ***
- set cpu affinity for worker 2 toTue Sep 23 16:51:19 2014 -  0Tue Sep 23 16:51:19 2014 -

谁能指出我哪里出错了?我花了几个小时尝试各种解决方案,但似乎没有任何效果,我一定遗漏了一些重要的东西但找不到它。

【问题讨论】:

    标签: python nginx flask uwsgi linode


    【解决方案1】:

    尝试添加&lt;callable&gt;application&lt;/callable&gt;

    【讨论】:

    • 还是一样。尝试使用 applicationapp 以防万一。
    • 哇,我成功了,我添加了&lt;module&gt;app&lt;/module&gt;&lt;callable&gt;app&lt;/callable&gt;,它可以工作了。有谁知道这是为什么,为什么必须这样定义?
    • uWSGI 查找名为 application 的可调用对象。如果你叫它别的名字,它需要知道它的名字是什么。
    猜你喜欢
    • 2013-09-01
    • 2019-04-05
    • 1970-01-01
    • 2018-02-02
    • 2021-01-27
    • 2019-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多