【问题标题】:Flask SocketIO & uWSGI causes 'TypeError: 'SocketIO' object is not callable'烧瓶 SocketIO 和 uWSGI 导致'TypeError:'SocketIO'对象不可调用'
【发布时间】:2017-07-11 13:13:20
【问题描述】:

我目前正在构建一个应用程序,它使用 Flask 和 Flask SocketIO 的组合来处理 HTTP 和 WebSocket 流量的组合。在服务器上部署我的应用程序并添加 uWSGI 和 Nginx 作为网关后,wsgi 脚本在日志中显示以下错误:

TypeError: 'SocketIO' object is not callable
[pid: 4262|app: 0|req: 3/8] XXX.XXX.XXX.XXX () {46 vars in 912 bytes} [Tue Jul 11 14:57:25 2017] GET / => generated 0 bytes in 0 msecs (HTTP/2.0 500) 0 

uWSGI documentation 中所述,我在配置文件中将http-websockets 标志设置为true。 Flask SocketIO 文档声明他们支持 uWSGI:

另一种选择是使用带有 WebSocket 功能的 uWSGI Web 服务器。使用 gevent 也是一种性能选择,但比 eventlet 略低。

有谁知道我做错了什么?我正在使用最新版本的 Nginx。我在这里先向您的帮助表示感谢。我的设置:

烧瓶服务器(mymod.server)

from flask import Flask
from flask_socketio import SocketIO, emit


server = Flask(__name__)
io = SocketIO(server)

@server.route("/")
def index():
    return server.send_static_file("index.html")

@io.on("ping")
def ping():
    emit("pong")

WSGI 脚本(项目的根目录)

from mymod.server import io

if __name__ == "__main__":
    io.run(host="127.0.0.1", port=8080)

WSGI 配置

# Configuration file for Nginx
[uwsgi]
module = wsgi:io
master = true
processes = 5
buffer-size=32768
http-websockets = true
socket = server.sock
chmod-socket = 666
vacuum = true
die-on-term = true
logto = /var/log/uwsgi/%n.log

Nginx 配置

server {
    listen 443 http2 ssl;
    listen [::]:443 http2 ssl;

    server_name _;
    server_tokens off;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/home/user/project/server.sock;
    }

    location /socket.io/ {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_pass http://unix:/home/user/project/server.sock;
    }
}

我如何启动我的服务器

$ uwsgi --ini config.wsgi.ini

【问题讨论】:

    标签: python nginx socket.io uwsgi


    【解决方案1】:

    原来 Flask-SocketIO 只是挂载到原始的 app 对象上,替换以下内容:

    [uwsgi]
    module = wsgi:io
    

    收件人:

    [uwsgi]
    module = wsgi:app
    

    会工作,但 uwsgi 仍然对 SocketIO 的支持很差,据我所知,这使得 gunicorn 成为更好的选择。

    【讨论】:

      猜你喜欢
      • 2017-11-16
      • 1970-01-01
      • 2015-05-25
      • 2018-05-22
      • 2013-04-30
      • 1970-01-01
      • 1970-01-01
      • 2014-07-28
      • 1970-01-01
      相关资源
      最近更新 更多