简体   繁体   English

我应该如何使用 socketio 运行 Flask 服务器?

[英]How should I run flask server with socketio?

I've built simple webserver in flask according to this tutorial: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world Then I realized that I need websockets, because I need real-time communication between client and server.我根据本教程在烧瓶中构建了简单的网络服务器: https : //blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world然后我意识到我需要 websockets,因为我需要客户端和服务器之间的实时通信。 So I installed in my venv flask-socketio library.所以我安装在我的 venv flask-socketio 库中。 Then I modified my __init__.py file so it looks like that:然后我修改了我的 __init__.py 文件,看起来像这样:

from flask import Flask
from flask_socketio import SocketIO
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
import logging
from logging.handlers import RotatingFileHandler
import os


app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
socketio = SocketIO(app)

if not app.debug:
    if not os.path.exists('logs'):
        os.mkdir('logs')
    file_handler = RotatingFileHandler('logs/webserver.log', maxBytes=10240,
                                       backupCount=10)
    file_handler.setFormatter(logging.Formatter(
        '%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'))
    file_handler.setLevel(logging.INFO)
    app.logger.addHandler(file_handler)

    app.logger.setLevel(logging.INFO)
    app.logger.info('Reaction test startup')

from app import routes, models, errors

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

The problem is that now when I try to run my server using flask run, I get warning:问题是,现在当我尝试使用 Flask run 运行我的服务器时,我收到警告:

WARNING in __init__: Flask-SocketIO is Running under Werkzeug, WebSocket is not available. __init__ 中的警告:Flask-SocketIO 在 Werkzeug 下运行,WebSocket 不可用。

I saw other people had this problem too ( https://github.com/miguelgrinberg/Flask-SocketIO/issues/894 ), and that I should run my __init__.py script instead of flask run.我看到其他人也有这个问题( https://github.com/miguelgrinberg/Flask-SocketIO/issues/894 ),我应该运行我的 __init__.py 脚本而不是烧瓶运行。 However, when I try to start my server with "python app/__init__.py", my imports are failing, feels like all dependencies are lost.但是,当我尝试使用“python app/__init__.py”启动我的服务器时,我的导入失败了,感觉所有依赖项都丢失了。 At first I received message:起初我收到消息:

Traceback (most recent call last): File "app/ init .py", line 3, in from config import Config回溯(最近一次调用最后一次):文件“app/ init .py”,第 3 行,在 from config import Config

So I decided to basically rewrite whole config file into __init__.py script.所以我决定基本上将整个配置文件重写为 __init__.py 脚本。 This didn't help though, as after doing it i received message:但这并没有帮助,因为这样做后我收到了消息:

File "app/ init .py", line 37, in from app import routes, models, errors ModuleNotFoundError: No module named 'app'文件“app/ init .py”,第 37 行,来自应用程序导入路由、模型、错误 ModuleNotFoundError: No module named 'app'

So I guess rewriting contents of Config.py file wasn't the problem solver.所以我猜重写 Config.py 文件的内容不是问题解决者。 My question is, how should I run my app so that it supports websockets properly without rewriting it from scratch?我的问题是,我应该如何运行我的应用程序,以便它正确支持 websockets 而无需从头开始重写它?

I solved my problem already.我已经解决了我的问题。 The problem was I tried to edit my __init__.py file instead of the one which was bound to FLASK_APP environmental variable.问题是我试图编辑我的 __init__.py 文件而不是绑定到 FLASK_APP 环境变量的文件。 When I added:当我添加:

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

to my FLASK_APP script and then ran this other script from console with python server.py (this is the name of my script bound to FLASK_APP variable) everything worked just fine.到我的 FLASK_APP 脚本,然后使用python server.py从控制台运行另一个脚本(这是绑定到 FLASK_APP 变量的脚本的名称)一切正常。

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

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