30

I try to create a simple flask app:

from flask import Flask

app = Flask(__name__)

if __name__ == '__main__':
  app.run()

but when I add the debug:

FLASK_APP = run.py
FLASK_ENV = development
FLASK_DEBUG = 1

I got the following error:

ValueError: signal only works in main thread

here the full stacktrace

FLASK_APP = run.py
FLASK_ENV = development
FLASK_DEBUG = 1
In folder c:/MyProjectPath/api
c:\MyProjectPath\api\venv\Scripts\python.exe -m flask run
 * Serving Flask-SocketIO app "run.py"
 * Forcing debug mode on
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 283-122-745
Exception in thread Thread-1:
Traceback (most recent call last):
  File "c:\appdata\local\programs\python\python37\Lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "c:\appdata\local\programs\python\python37\Lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "c:\MyProjectPath\api\venv\lib\site-packages\flask_socketio\cli.py", line 59, in run_server
    return run_command()
  File "c:\MyProjectPath\api\venv\lib\site-packages\click\core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "c:\MyProjectPath\api\venv\lib\site-packages\click\core.py", line 717, in main
    rv = self.invoke(ctx)
  File "c:\MyProjectPath\api\venv\lib\site-packages\click\core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "c:\MyProjectPath\api\venv\lib\site-packages\click\core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "c:\MyProjectPath\api\venv\lib\site-packages\click\decorators.py", line 64, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "c:\MyProjectPath\api\venv\lib\site-packages\click\core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "c:\MyProjectPath\api\venv\lib\site-packages\flask\cli.py", line 771, in run_command
    threaded=with_threads, ssl_context=cert)
  File "c:\MyProjectPath\api\venv\lib\site-packages\werkzeug\serving.py", line 812, in run_simple
    reloader_type)
  File "c:\MyProjectPath\api\venv\lib\site-packages\werkzeug\_reloader.py", line 267, in run_with_reloader
    signal.signal(signal.SIGTERM, lambda *args: sys.exit(0))
  File "c:\appdata\local\programs\python\python37\Lib\signal.py", line 47, in signal
    handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler))
ValueError: signal only works in main thread
Frennetix
  • 3,269
  • 5
  • 15
  • 23
  • I came here trying to set a breakpoint in Django code. There are places where it [doesn't work](https://github.com/jonathanslenders/ptpdb/issues/7#issuecomment-508928443). There are also some ideas how to work around it. – x-yuri Jul 06 '19 at 13:57

2 Answers2

44

The problem you are facing has to do with a bug in the Flask-SocketIO package which replaces the flask run command. Due to this Flask-SocketIO is always used even if you don’t import it. There are several solutions:

  1. Uninstall Flask-SocketIO
  2. Do not use flask run but run the main file of your program
  3. Disable debugging
  4. Disable auto loading if debugging required flask run --no-reload

Reference to the Flask-SocketIO bug: issue 817

rfkortekaas
  • 6,049
  • 2
  • 27
  • 34
  • 1
    I changed my config to use python directly in my IDE and it works, you're right after verification Flask-SocketIO is replacing my run, thank you. – Frennetix Nov 29 '18 at 09:45
  • @Frennetix - changed config to use python directly? May I know what you did exactly? Like python in terminal? – Rajesh Mappu Jan 11 '19 at 21:11
  • 2
    @RajeshMappu weirdly I just "python.exe run.py" – Frennetix Jan 28 '19 at 11:46
  • 1
    im having the same issue but Flask-SocketIO is not installed – Sonic Soul Feb 25 '19 at 15:57
  • @SonicSoul can you solve the issue by running the main file of the program instead of ‘flask run’ – rfkortekaas Feb 25 '19 at 17:18
  • @rfkortekaas not sure what you mean, but I can run a test.py while flask is running, and that returns regular results .. test.py has the same code as my flask endpoint accept all in one file instead of referencing modules etc.. – Sonic Soul Feb 25 '19 at 18:35
  • @rfkortekaas i am also able to bash into the container, and execute my flask endpoint code line by line inside python REPL. and it returns data from Athena – Sonic Soul Feb 25 '19 at 18:41
  • In case of Django that would be `./manage.py runserver --nothreading --noreload`. – x-yuri Jul 06 '19 at 10:15
  • @rfkortekaas When I try to uninstall it says you don't have Flask-socketio. – Prajwal Aug 19 '19 at 12:31
  • @Prajwal try `pip freeze` to verify if it’s installed and the name is correct. – rfkortekaas Aug 19 '19 at 15:13
  • And when you are running it through gunicorn or some other WSGI server in production? – Sam Redway Aug 20 '19 at 13:18
  • I am using socketIO. Is there another way to get hot reloading of a flask app? – dcsan Jan 01 '20 at 19:46
  • I'm running using python directly in my IDE (PyCharm) but still getting this error.. and Flask-SocketIO is not installed.. any suggestion? thanks – A.K Apr 03 '21 at 21:14
1

I solved the problem thanks to @AkshayKumar007 answer on github. That was the most convenient solution for me.

Hey guys, I was also facing the same problem. So to summarize, if you're using socket-io, don't do flask run. First, add

if __name__ == "__main__":
    socketio.run(app)

At the end of your application. To run it just do

python3 __init__.py

Hope it helped.

snoob dogg
  • 2,491
  • 3
  • 31
  • 54