简体   繁体   English

Flask SocketIO和uWSGI导致“ TypeError:'SocketIO'对象不可调用”

[英]Flask SocketIO & uWSGI causes 'TypeError: 'SocketIO' object is not callable'

I'm currently building an app that uses a combination of Flask and Flask SocketIO to handle a combination of HTTP and WebSocket traffic. 我目前正在构建一个结合使用Flask和Flask SocketIO来处理HTTP和WebSocket流量组合的应用程序。 After deploying my application on a server and adding uWSGI and Nginx as a gateway, the wsgi script gives me the following error in the logs: 在将我的应用程序部署到服务器上并将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 

As stated in the uWSGI documentation , I added set http-websockets flag to true in my configuration file. uWSGI文档中所述,我在配置文件中添加了将set http-websockets标志设置为true的功能。 The Flask SocketIO documentation states that they have support for uWSGI: Flask SocketIO文档声明他们支持uWSGI:

The other alternative is to use the uWSGI web server, which comes with WebSocket functionality. 另一种选择是使用带有WebSocket功能的uWSGI Web服务器。 The use of gevent is also a performant option, but slightly lower than eventlet. gevent的使用也是一个性能选项,但比eventlet略低。

Does anyone know what I'm doing wrong? 有人知道我在做什么错吗? I'm using the latest version of Nginx. 我正在使用最新版本的Nginx。 Thanks for the help in advance. 我在这里先向您的帮助表示感谢。 My setup: 我的设置:

Flask server (mymod.server) 烧瓶服务器(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 script (root of the project) WSGI脚本(项目的根)

from mymod.server import io

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

WSGI config 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 config 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;
    }
}

How I start my server 我如何启动服务器

$ uwsgi --ini config.wsgi.ini

Turns out Flask-SocketIO simply mounts onto the original app object, replacing the following: 事实证明,Flask-SocketIO可以简单地安装到原始app对象上,替换以下内容:

[uwsgi]
module = wsgi:io

To: 至:

[uwsgi]
module = wsgi:app

Would work, but uwsgi still has terrible support for SocketIO, making gunicorn a better option as from what I've heard. 可以,但是uwsgi对SocketIO仍然有糟糕的支持,从我所听说的情况来看,使gunicorn成为更好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM