Reputation:
I would like to run two servers in parallel when my application starts, one for HTTP
requests, one for websockets
(using python-sockio). I already have a python back-end which starts an HTTP
-based server with Flask
. However, every time I start the first server, it seems like it is blocking the thread which in turn, causes my second server not to initialize at all. Since I'm using Flask, is there a Flask
-way to do this?
Upvotes: 1
Views: 1685
Reputation: 1744
Sounds like an XY problem. Your app can serve both websockets and HTTP. Here's a code example taken from Miguel Grinberg's Flask-Socketio example:
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
@app.route('/')
def index():
return render_template('index.html')
@socketio.on('my event')
def test_message(message):
emit('my response', {'data': 'got it!'})
if __name__ == '__main__':
socketio.run(app)
Upvotes: 0
Reputation: 79
you can try to run the two servers on 2 different ports like so
flask run --host 0.0.0.0 --port 5000
flask run --host 0.0.0.0 --port 5001
If I did a terrible job explaining try and look at this thread Python - How to run multiple flask apps from same client machine
Upvotes: 2