7

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?

Cedric Reichenbach
  • 8,970
  • 6
  • 54
  • 89
  • `if __name__ == "__main__"` will only be executed when you execute that file, not when you import it from somewhere else. `python -m flask` looks for a variable called `app` or `application` or for a flask object. Is there a reason why you don't launch via `python mymainfile.py`? – syntonym Nov 21 '16 at 18:07
  • That makes sense to me. Flask-SocketIO's docs have a [short paragraph on `flask run`](http://flask-socketio.readthedocs.io/en/latest/#initialization), but I don't understand how it's supposed to work exactly. The launch code is executed from a rather complex Node script I didn't create, but I can't see the motivation of that over `python x.py`. – Cedric Reichenbach Nov 21 '16 at 19:43

1 Answers1

4

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.

Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152
  • Thanks, that was the missing piece! I didn't see anywhere in the docs that `flask run` is actually changed by Flask-SocketIO. Also, that explains why a previous flag (`--with-threads`) suddenly stopped working. By any chance, do you happen to know where this flag is documented, as I couldn't find it either... – Cedric Reichenbach Nov 22 '16 at 07:46
  • 1
    @CedricReichenbach To make Flask-SocketIO work with the Flask server, there are some restrictions that need to be imposed. In particular, while Flask gives you an option to use `--with-threads` on a regular server, when using Flask-SocketIO that option is not available. Flask-SocketIO requires threads to be enabled, so the option was removed and threads are enabled internally. – Miguel Grinberg Nov 22 '16 at 17:10
  • Ah, I see. Thanks a lot! – Cedric Reichenbach Nov 22 '16 at 20:05