Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
766 views
in Technique[技术] by (71.8m points)

python - Flask APP - ValueError: signal only works in main thread

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:MyProjectPathapivenvScriptspython.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:appdatalocalprogramspythonpython37Libhreading.py", line 917, in _bootstrap_inner
    self.run()
  File "c:appdatalocalprogramspythonpython37Libhreading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "c:MyProjectPathapivenvlibsite-packagesflask_socketiocli.py", line 59, in run_server
    return run_command()
  File "c:MyProjectPathapivenvlibsite-packagesclickcore.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "c:MyProjectPathapivenvlibsite-packagesclickcore.py", line 717, in main
    rv = self.invoke(ctx)
  File "c:MyProjectPathapivenvlibsite-packagesclickcore.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "c:MyProjectPathapivenvlibsite-packagesclickcore.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "c:MyProjectPathapivenvlibsite-packagesclickdecorators.py", line 64, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "c:MyProjectPathapivenvlibsite-packagesclickcore.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "c:MyProjectPathapivenvlibsite-packagesflaskcli.py", line 771, in run_command
    threaded=with_threads, ssl_context=cert)
  File "c:MyProjectPathapivenvlibsite-packageswerkzeugserving.py", line 812, in run_simple
    reloader_type)
  File "c:MyProjectPathapivenvlibsite-packageswerkzeug\_reloader.py", line 267, in run_with_reloader
    signal.signal(signal.SIGTERM, lambda *args: sys.exit(0))
  File "c:appdatalocalprogramspythonpython37Libsignal.py", line 47, in signal
    handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler))
ValueError: signal only works in main thread
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...