2

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"
Cleb
  • 25,102
  • 20
  • 116
  • 151
R. Gggs
  • 33
  • 2
  • Possible duplicate of [Flask APP - ValueError: signal only works in main thread](https://stackoverflow.com/questions/53522052/flask-app-valueerror-signal-only-works-in-main-thread) – RaminNietzsche Feb 17 '19 at 14:24
  • I have read that thread, however it didn't help since I am not able to use SocketIO while debug mode is on on my flask server. – R. Gggs Feb 17 '19 at 14:27
  • I was unable to reproduce the problem. Maybe you should include details such as python version, version of Flask and version of Flask-SocketIO – J.J. Hakala Feb 17 '19 at 15:40

2 Answers2

2

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.
AsifB
  • 21
  • 2
0

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
Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152