首页 > 解决方案 > 通过 uwsgi 启动 Flask 时没有加载应用程序

问题描述

我知道这是一个常见问题,但由于我已经搜索了 3 个小时,并且考虑到必须涉及量子奇点,我不得不发布这个。

我不断收到这个熟悉的错误:

[uWSGI] getting INI configuration from uwsgi.ini,
*** Starting uWSGI 2.0.18-debian (64bit) on [Mon May 25 15:56:04 2020] ***,
compiled with version: 8.2.0 on 10 February 2019 02:42:46,
os: Linux-5.6.12-1-MANJARO #1 SMP PREEMPT Sun May 10 14:36:43 UTC 2020,
nodename: a43c6a7bbb2c,
machine: x86_64,
clock source: unix,
pcre jit disabled,
detected number of CPU cores: 8,
current working directory: /srv/myapp,
detected binary path: /usr/bin/uwsgi-core,
setgid() to 33,
setuid() to 33,
your memory page size is 4096 bytes,
detected max file descriptor number: 1048576,
lock engine: pthread robust mutexes,
thunder lock: disabled (you can enable it with --thunder-lock),
uwsgi socket 0 bound to UNIX address /tmp/uwsgi.socket fd 3,
your server socket listen backlog is limited to 100 connections,
your mercy for graceful operations on workers is 60 seconds,
mapped 437520 bytes (427 KB) for 5 cores,
*** Operational MODE: preforking ***,
*** no app loaded. going in full dynamic mode ***,
*** uWSGI is running in multiple interpreter mode ***,
!!!!!!!!!!!!!! WARNING !!!!!!!!!!!!!!,
no request plugin is loaded, you will not be able to manage requests.,
you may need to install the package for your language of choice, or simply load it with --plugin.,
!!!!!!!!!!! END OF WARNING !!!!!!!!!!,
spawned uWSGI master process (pid: 25),
spawned uWSGI worker 1 (pid: 28, cores: 1),
spawned uWSGI worker 2 (pid: 29, cores: 1),
spawned uWSGI worker 3 (pid: 30, cores: 1),
spawned uWSGI worker 4 (pid: 31, cores: 1),
spawned uWSGI worker 5 (pid: 32, cores: 1),

但是,我认为这个非常简单的设置没有任何问题。我的设置:

核心/__init__.py

from flask import Flask

def create_app():

    app = Flask(__name__)

    @app.route('/')
    def hello_world():
        return 'Hello, World!'

    return app

应用程序.py

from core import create_app

app = create_app()

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

Dockerfile

FROM python:3.6-slim

RUN apt-get clean \
    && apt-get -y update
RUN apt-get -y install uwsgi nginx

COPY . /srv/myapp
COPY nginx.conf /etc/nginx
WORKDIR /srv/myapp

RUN pip install -r requirements.txt

RUN chmod +r uwsgi.ini
RUN chmod +x ./start.sh
CMD ["./start.sh"]

启动.sh

#!/usr/bin/env bash
service nginx start
uwsgi --ini uwsgi.ini

uwsgi.ini

[uwsgi]
module = app:app
uid = www-data
gid = www-data
master = true
processes = 5
socket = /tmp/uwsgi.socket
chmod-sock = 664
vacuum = true
die-on-term = true

nginx.conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;events 
{
    worker_connections 1024;
    use epoll;
    multi_accept on;
}
http {
    access_log /dev/stdout;
    error_log /dev/stdout;    
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;    
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;    
    index   index.html index.htm;    
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  localhost;
        root         /var/www/html;        
        location / 
        {
            include uwsgi_params;
            uwsgi_pass unix:/tmp/uwsgi.socket;
        }
    }
}

如果我在 DOCKERFILE 中添加 python3 插件RUN apt-get -y install uwsgi nginx uwsgi-plugin-python3并通过运行 uwsgiuwsgi --ini uwsgi.ini --plugin python3我得到:

Traceback (most recent call last):
  File "./app.py", line 1, in <module>
    from core import create_app
  File "./core/__init__.py", line 1, in <module>
    from flask import Flask
ModuleNotFoundError: No module named 'flask'

我不知道这是否是一个暗示。任何帮助表示赞赏!谢谢!

标签: pythonpython-3.xflaskdockerfileuwsgi

解决方案


睡了8个小时后,我发现了我的错误:

通过安装 uwsgiapt-get将不起作用。需要通过pip install. (我的猜测是,这是两个不同的 uwsgi 实例/服务)


推荐阅读