Reputation: 4178
I started with an application that has several webservices defined. I was able to start the application via flask run
on the command line. Afterwards, I integrated flask-sckoetio (i.e. I added the lines from flask_socketio import SocketIO, emit
and socketio = SocketIO(app)
) and now I'm not able anymore to start the server via flask run
.
from flask import Flask, request, abort
from flask_socketio import SocketIO, emit
app = Flask(__name__)
socketio = SocketIO(app)
@app.route('/do_sth', methods=['POST'])
def do_sth():
return ""
I get the following message on the console:
* Serving Flask-SocketIO app "webservices.py"
* Forcing debug mode off
WebSocket transport not available. Install eventlet or gevent and gevent-websocket for improved perform
ance.
c:\program files\python36\lib\site-packages\flask_socketio\__init__.py:496: Warning: Silently ignoring
app.run() because the application is run from the flask command line executable. Consider putting app.
run() behind an if __name__ == "__main__" guard to silence this warning.
use_reloader=use_reloader, **kwargs)
So I updated my code to this:
from flask import Flask, request, abort
from flask_socketio import SocketIO, emit
app = Flask(__name__)
socketio = SocketIO(app)
@app.route('/do_sth', methods=['POST'])
def do_sth():
return ""
if __name__ == '__main__':
socketio.run(app)
But I still get the same error message and the server doesn't start. However, if I just execute the script everything works. But why is flask run
not possible anymore?
Upvotes: 4
Views: 5250
Reputation: 1909
It happens because of variable __name__
is equal to "__main__"
only when the file called directly with a command like python file.py
. But your file was imported and his __name__
variable is setted to name of a module which import them.
Solution:
Just delete string if __name__ == "__main__":
Upvotes: 1
Reputation: 11
Did you install eventlet or gevent, which are mentioned in the error message?
They are also listed under requirements in flask-socketIO documentation. Try installing them first.
After installing one of them, you can just use flask run
to start your application.
Upvotes: 0