Reputation: 33
I am trying to set up a Flask server which can use SocketIO
, however it doesn't work and it returns me this following error:
ValueError: signal only works in main thread
This is my setup for the flask environment:
export FLASK_APP=application.py
export FLASK_DEBUG=1
Then I run like I would normally do, and would work before I started using SocketIO
:
flask run
Here is my code for application.py
, which is very simple but maybe it helps:
import os
from flask import Flask
from flask_socketio import SocketIO, emit
app = Flask(__name__)
app.config["SECRET_KEY"] = os.getenv("SECRET_KEY")
socketio = SocketIO(app)
@app.route("/")
def index():
return "Hello, world"
Upvotes: 2
Views: 991
Reputation: 21
I am playing with flask_socketio just to understand how it works. So my workaround might not be ideal.
I ran following the terminal
$ export FLASK_ENV=production
$ flask run
I got following warning but my test application worked
Serving Flask app "<applicationname>.py"
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
[2019-03-20 09:58:09,131] WARNING in __init__: Flask-SocketIO is Running under Werkzeug, WebSocket is not available.
Upvotes: 2
Reputation: 67479
In current releases of Flask-SocketIO the flask run
method to start the server can only be used when working with the Flask development server, which is not recommended because it does not support WebSocket.
What I recommend that you do is that you change your application as follows:
import os
from flask import Flask
from flask_socketio import SocketIO, emit
app = Flask(__name__)
app.config["SECRET_KEY"] = os.getenv("SECRET_KEY")
socketio = SocketIO(app)
@app.route("/")
def index():
return "Hello, world"
if __name__ == '__main__':
socketio.run(app, debug=True)
And then run the application with:
python application.py
Upvotes: 0