如何提高 Flask socketio 服务器的性能并对其进行负载测试?线程还是多处理?
我有一个简单的 Flask socketio 服务器,它每 2.5 毫秒连续接收来自客户端的音频输入流。 Flask 服务器将该音频流传递给机器学习模型,该模型检测音频并为我们提供结果。
我的代码如下所示
from argparse import Namespace
from flask import Flask
from flask_socketio import SocketIO, emit
from flask_cors import CORS, cross_origin
import detector
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
socketio = SocketIO(app, cors_allowed_origins="*")
@cross_origin()
@socketio.on('stream')
def test_message(message):
result = detector.detect(message['data']) # process which can take some time to run
return result
if __name__ == '__main__':
socketio.run(app,host='0.0.0.0')
现在假设有 1000 个客户端连接到此,并且它们将大约 80k-100k 字节的 2.5 ms 音频流传输到服务器。我如何确保我的服务器可以处理这个问题?我需要实现线程或多处理吗?我是否需要在单独的线程中为每个客户端运行套接字?如何对该服务器进行负载测试?
I have a simple flask socketio server that continuously takes in streams of audio input from a client for every 2.5ms. The flask server passes that stream of audio to an ML model which detects the audio and gives us a result.
My code looks like this
from argparse import Namespace
from flask import Flask
from flask_socketio import SocketIO, emit
from flask_cors import CORS, cross_origin
import detector
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
socketio = SocketIO(app, cors_allowed_origins="*")
@cross_origin()
@socketio.on('stream')
def test_message(message):
result = detector.detect(message['data']) # process which can take some time to run
return result
if __name__ == '__main__':
socketio.run(app,host='0.0.0.0')
Now let's assume there are 1000 clients connected to this and they are streaming around 80k-100k bytes of 2.5 ms audio to the server. How do I make sure my server can handle this? Do I need to implement threading or multiprocessing? Do I need to run the socket for each client in a separate thread? How do I load test this server?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论