0

我正在使用 Flask-SocketIO、带有 Gevent 的 uWSGI 和 Nginx 来开发 WebSocket 项目并将这个应用程序 docker 化。我的应用程序的简单运行很好,但是当我使用 Gevent 添加 uWSGI 和 Nginx 时,它给了我一个错误,即TypeError: 'module' object is not callable。我搜索了互联网,但没有任何有用的材料。我应该怎么做才能解决这个问题?我的烧瓶项目结构是:

vsoTS
│
├───app
│   │   views.py
│   │   __init__.py
│   ├───static
│   │    └───js
│   │    │      application.js
│   │    ├───templates
│   │    │      index.html
├───env
│  .dockerignore
│   app.ini
│   Dockerfile
│   requirements.txt
│   run.py

运行.py

from gevent import monkey
monkey.patch_all()
from app import app
from app import socketio
print(type(socketio))
sock = socketio
if __name__ == "__main__":
    sock.run(host='0.0.0.0', port = 8084)

初始化.py

from flask import Flask
from flask_socketio import SocketIO
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app ,logger=True, engineio_logger=True)
from app import views

nginx.conf

server{    
    location /vsoTS {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://0.0.0.0:8084;
    }

    location /socket.io {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        proxy_http_version 1.1;
        proxy_buffering off;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_pass http://0.0.0.0:8084/socket.io;
    }

}

uWSGI 配置位于app.ini

[uwsgi]
gevent-monkey-patch = true
wsgi-file = run.py
callable = app
processes = 4
threads = 2
master = true
chmod-socket = 660
vacuum = true
mount = /=run.py
manage-script-name = true
die-on-term = true
py-call-osafterfork = true
need-app = true
enable-threads = true
strict = true
buffer-size=32768
gevent = 1000
http-websockets = true
http-socket = 0.0.0.0:8084
single-interpreter = true

在 Docker 容器中运行烧瓶应用程序后,它向我显示了TypeError: 'module' object is not callable下面给出的详细日志。

vsots       | [uWSGI] getting INI configuration from app.ini
vsots       | *** Starting uWSGI 2.0.18 (64bit) on [Fri Jul 16 00:01:07 2021] ***
vsots       | compiled with version: 8.3.0 on 16 July 2021 00:00:39
vsots       | os: Linux-5.4.72-microsoft-standard-WSL2 #1 SMP Wed Oct 28 23:40:43 UTC 2020
vsots       | nodename: e1f246977cb5
vsots       | machine: x86_64
vsots       | clock source: unix
vsots       | pcre jit disabled
vsots       | detected number of CPU cores: 8
vsots       | current working directory: /app
vsots       | detected binary path: /usr/local/bin/uwsgi
vsots       | uWSGI running as root, you can use --uid/--gid/--chroot options
vsots       | *** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
vsots       | your memory page size is 4096 bytes
vsots       | detected max file descriptor number: 1048576
vsots       | - async cores set to 1000 - fd table size: 1048576
vsots       | lock engine: pthread robust mutexes
vsots       | thunder lock: disabled (you can enable it with --thunder-lock)
vsots       | uwsgi socket 0 bound to TCP address 0.0.0.0:8084 fd 3
vsots       | uWSGI running as root, you can use --uid/--gid/--chroot options
vsots       | *** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
vsots       | Python version: 3.9.5 (default, Jun 23 2021, 15:01:51)  [GCC 8.3.0]
vsots       | Python main interpreter initialized at 0x5627d11d8810
vsots       | uWSGI running as root, you can use --uid/--gid/--chroot options
vsots       | *** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
vsots       | python threads support enabled
vsots       | your server socket listen backlog is limited to 100 connections
vsots       | your mercy for graceful operations on workers is 60 seconds
vsots       | mapped 703600 bytes (687 KB) for 8 cores
vsots       | *** Operational MODE: preforking+threaded ***
vsots       | Server initialized for eventlet.
vsots       | Server initialized for eventlet.
vsots       | <class 'flask_socketio.SocketIO'>
vsots       | WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x5627d11d8810 pid: 1 (default app)
vsots       | mounting run.py on /
vsots       | <class 'flask_socketio.SocketIO'>
vsots       | WSGI app 1 (mountpoint='/') ready in 0 seconds on interpreter 0x5627d11d8810 pid: 1
vsots       | uWSGI running as root, you can use --uid/--gid/--chroot options
vsots       | *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
vsots       | spawned uWSGI master process (pid: 1)
vsots       | spawned uWSGI worker 1 (pid: 8, cores: 2)
vsots       | spawned uWSGI worker 2 (pid: 9, cores: 2)
vsots       | *** running gevent loop engine [addr:0x5627cf7f1cf0] ***
vsots       | TypeError: 'module' object is not callable
vsots       | TypeError: 'module' object is not callable
vsots       | TypeError: 'module' object is not callable
vsots       | spawned uWSGI worker 3 (pid: 10, cores: 2)
vsots       | TypeError: 'module' object is not callable
vsots       | TypeError: 'module' object is not callable
vsots       | TypeError: 'module' object is not callable
vsots       | spawned uWSGI worker 4 (pid: 11, cores: 2)
vsots       | TypeError: 'module' object is not callable
vsots       | TypeError: 'module' object is not callable
vsots       | TypeError: 'module' object is not callable
vsots       | TypeError: 'module' object is not callable
vsots       | TypeError: 'module' object is not callable
vsots       | TypeError: 'module' object is not callable
4

1 回答 1

0

我的代码的第一个问题是,我是SocketIO从原始flask_socketio库中导入的,并且我还将它导入到__init__.py我的 app 文件夹的文件中。另一方面,我正在从文件中导入 socketio 对象,__init__.py因此run.py从库中导入对象时存在冲突,这就是TypeError: 'module' object is not callable引发错误的原因。下面给出了我的问题的解决方案。

初始化.py

from flask import Flask, render_template
from flask_socketio import SocketIO, emit
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
app.config['DEBUG'] = True
app.config['SERVER_NAME'] = None
socketio = SocketIO(app, cors_allowed_origins='*', async_mode=None, logger=True, engineio_logger=True)
from app import views

视图.py

from app import app, render_template
from app import socketio, emit
import os
from random import random
from time import sleep
from threading import Thread, Event

运行.py

from app import app

这是flask-socketio应用程序。运行我使用的应用程序,gunicorn而不是uWSGI在此之上,它实际上作为nginxflask_socketio应用程序之间的桥梁,两者都在不同的 docker 容器中运行。 docker容器中flask socketio应用程序的gunicorn配置是,

CMD ["gunicorn", "run:app", "--bind", "0.0.0.0:8084", "--worker-class", "eventlet", "--workers", "1", "--log-level", "DEBUG"]

我的代码的第二个问题是在单独的 docker 容器中运行的 nginx 的配置。我正在代理将 nginx 传递给 http 套接字地址,在我的情况下proxy_pass http://0.0.0.0:8084;proxy_pass http://0.0.0.0:8084/socket.io;这些传递是错误的。正确的配置是代理传递到您的烧瓶 socketio 所在的实际 docker 容器,并在其之上使用 gunicorn 运行此应用程序。就我而言,docker 容器vsots8084. 在 docker 容器中运行的套接字应用程序的 nginx 配置是,

upstream vsoTS_http_node{
    server vsots:8084;
}
upstream vsoTS_socketio_nodes{
    ip_hash;
    server vsots:8084;
}
server {
    
    listen 80;
    server_name _;
    
    location /vsoTS {
        proxy_pass http://vsoTS_http_node;
    }
    location /vsoTS/socket.io {
        proxy_http_version 1.1;
        proxy_buffering off;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_pass http://vsoTS_socketio_node/socket.io;
    }

}

通过 nginx 在 docker 容器中运行的烧瓶套接字应用程序的屏幕截图。 我正在运行的套接字应用程序的屏幕截图

于 2021-07-31T21:20:32.927 回答