I'm working with a flask app that's currently launched as follows:
The starting command is python -m flask run
, where FLASK_APP
pointing to __init.py__
.
__init.py__
simply sets a variable app
to an instance of Flask
:
from .app import get_app
app = get_app()
if __name__ == "__main__":
# do nothing
pass
It seems to me that flask detects global instances of Flask
set in this module and runs them, even though I couldn't find any documentation on this.
Now I'd like to integrate Flask-SocketIO, which requires the flask app to be wrapped and the socket instance to be run. From the docs, it seems that I should be able to run it from main:
from .app import get_app
app, sio = get_app() # returns a Flask and a SocketIO instance now
if __name__ == "__main__":
sio.run(app)
print("Flask-SocketIO server launched")
But I never see the expected output, and the socket server doesn't seem to be running. To me, this sounds like flask is ignoring the main function and still just launching any Flask
instance it finds.
Why is this happening, i.e. is there any documentation on this? Or, am I doing the Flask-SocketIO integration wrong?
If you are using Flask 0.11 and the new cli, then all you need to do to run your application is flask run
. Flask-SocketIO overrides the implementation of this command and adds the necessary magic to make everything work.
And you can remove your if __name__ == '__main__'
block, unless you also want to be able to start the server using the old pre-Flask 0.11 way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With