Python Flask APP - ValueError:信号仅适用于主线程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53522052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Flask APP - ValueError: signal only works in main thread
提问by Frennetix
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
ValueError:信号仅适用于主线程
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
回答by rfkortekaas
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:
您面临的问题与 Flask-SocketIO 包中的一个错误有关,该错误替换了该flask run
命令。因此,即使您不导入 Flask-SocketIO,也始终会使用它。有几种解决方案:
- Uninstall Flask-SocketIO
- Do not use
flask run
but run the main file of your program - Disable debugging
- Disable auto loading if debugging required
flask run --no-reload
- 卸载 Flask-SocketIO
- 不要使用
flask run
而是运行程序的主文件 - 禁用调试
- 如果需要调试,请禁用自动加载
flask run --no-reload
Reference to the Flask-SocketIO bug: issue 817
参考 Flask-SocketIO 错误:问题 817
回答by snoob dogg
I solved the problem thanks to @AkshayKumar007 answeron github. That was the most convenient solution for me.
感谢 @AkshayKumar007在 github 上的回答,我解决了这个问题。这对我来说是最方便的解决方案。
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.
大家好,我也遇到了同样的问题。总而言之,如果您使用的是 socket-io,请不要执行 flask run。首先,添加
if __name__ == "__main__": socketio.run(app)
在您的申请结束时。运行它只是做
python3 __init__.py
希望它有所帮助。