socketio.AsyncServer() 和 Flask 集成

问题描述 投票:0回答:1

我想在 Flask 应用程序中使用 socketio.AsyncServer,但不知道如何集成它。 (SocketIO 对我来说效果很好)

这是部分代码

from flask import Flask
import socketio

app = Flask(__name__)

# Is configuration correct for Flask and socketio.AsyncServer?
sio = socketio.AsyncServer(async_mode='asgi', cors_allowed_origins="*") 

(how to integrate sio with app here?)

app.run(debug=True) 

如何将 sio 对象与 Flask app 对象集成?

这里有参考,但没有找到解决方案:

https://manpages.ubuntu.com/manpages/jammy/man1/python-socketio.1.html https://python-socketio.readthedocs.io/en/latest/server.html

flask flask-socketio
1个回答
0
投票

AFAIK,socketio 目前还没有与 Flask async 直接集成。因此,您要么需要将 Flask-SocketIO 与同步客户端集成(可以使用 gevent 或 eventlet 进行修补)。

它是您期望高负载的生产应用程序吗?如果没有,我肯定建议使用 Flask-SocketIO https://flask-socketio.readthedocs.io/en/latest/,因为它是满足简单 MVP 需求的非常方便的集成。

如果它是一个生产应用程序,并且您想避免额外的依赖项,并且不打算从 SocketIO 实例中的 Flask 身份验证中受益,我建议将 Flask 应用程序与 SocketIO 应用程序解耦,并使用异步客户端来部署你的选择https://python-socketio.readthedocs.io/en/stable/server.html#uvicorn-daphne-and-other-asgi-servers.

© www.soinside.com 2019 - 2024. All rights reserved.