Flask-SocketIO 启动报错 ValueError: signal only works in main thread

之前一直正常,我装了一个json处理包,jsonpath之后,flask—socketio起不来。提示使用的是Werkzeug服务器而不是eventlet。

关闭DEBUG模式
WARNING in init: Flask-SocketIO is Running under Werkzeug, WebSocket is not available.

开启debug模式:

我谷歌了一下,作者给出的答案是卸载重装一下,我试图重装了flask—socketio和eventlet,还是启动不了,不知道啥原因!

我的启动方式是没问题的,socketio.run(app)

请使用文本的形式给出相关代码、相关依赖包的版本、完整的错误输出等信息。具体发帖规则见论坛说明

依赖包文件:


[[source]]
name = “pypi”
url = “Simple index
verify_ssl = true

[dev-packages]

[packages]
flask = “"
flask-script = "

flask-migrate = “"
flask-login = "

flask-wtf = “"
requests = "

flask-openid = “"
flask-mail = "

python-alipay-sdk = “"
flask-socketio = "

pymysql = “"
eventlet = "

[requires]
python_version = “3.6”

——————————————————————————————————
初始化文件:


from flask import Flask
from flask_socketio import SocketIO
from apps import config
from flask_mail import Mail
from apps.models.base import db
from flask_login import LoginManager

import apps.models.data

from os.path import dirname, join

def create_app():
app = Flask(name)
app.config.from_object(“apps.config”)
db.init_app(app)
socketio.init_app(app)
login_manager.init_app(app)
login_manager.login_view = ‘home.login’

with app.app_context():
db.create_all()

from apps.website.home import home_bp
from apps.website.auth import auth_bp
from apps.website.chat import chat_bp
from apps.website.pay import pay_bp
from apps.server.admin import admin_bp

app.register_blueprint(home_bp)
app.register_blueprint(admin_bp, url_prefix=“/server”)
app.register_blueprint(auth_bp, url_prefix=‘/auth’)
app.register_blueprint(chat_bp, url_prefix=‘/chat’)
app.register_blueprint(pay_bp, url_prefix=‘/pay’)
return app

mail = Mail()
socketio = SocketIO()
login_manager = LoginManager()
login_manager.session_protection = ‘strong’

——————————————————————————————————————
启动文件:


from apps import create_app, db,socketio
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand

app = create_app()
manager = Manager(app)
migrate = Migrate(app, db)
manager.add_command(‘db’, MigrateCommand)

if name == ‘main’:
socketio.run(app)

——————————————————————————
关闭DEBUG调试:
关闭DEBUG能正常连接,就是提示使用的是内置开发服务器。

————————————————————————————
开启DEBUG 调试
错误:
handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler))
ValueError: signal only works in main thread

之前开启debug模式是正常,装了一个jsonpath包,就报错了。后来卸载了jsonpath还是报错,不知道咋回事。查了很多资料,感觉都不行啊。:joy:,给整懵逼了

这是我的flask_socketio版本:
Flask-SocketIO==3.2.1

  • Flask [required: >=0.9, installed: 1.0.2]
    • click [required: >=5.1, installed: 7.0]
    • itsdangerous [required: >=0.24, installed: 1.1.0]
    • Jinja2 [required: >=2.10, installed: 2.10]
      • MarkupSafe [required: >=0.23, installed: 1.1.0]
    • Werkzeug [required: >=0.14, installed: 0.14.1]
  • python-socketio [required: >=2.1.0, installed: 3.1.2]
    • python-engineio [required: >=3.2.0, installed: 3.3.0]
      • six [required: >=1.9.0, installed: 1.12.0]
    • six [required: >=1.9.0, installed: 1.12.0]

eventlet==0.24.1

  • dnspython [required: >=1.15.0, installed: 1.16.0]
  • greenlet [required: >=0.3, installed: 0.4.15]
  • monotonic [required: >=1.4, installed: 1.5]
  • six [required: >=1.10.0, installed: 1.12.0]

发现几个问题:

  • socketio.run(app)flask run 是互相替代的两种启动方式,不要同时使用,在新版本里使用前者启动服务器。
  • FLASK_DEBUGFLASK_ENV 也类似替代关系(两者都控制调试模式),使用后者即可。

发帖时不要使用图片给出代码,具体规则参考论坛说明

关于使用 flask-socketio 插件在使用flask run启动,出现

ValueError: signal only works in main thread

异常的问题。如果非要flask run启动,试试下面的方案:

首先更新Flask-SocketIO到3.2.0 Release版本以上。然后试试:

flask run --no-reload

切记一定要带 --no-reload 参数,本人测试已经成功。


参考资料:

2 个赞

在pycharm中添加参数,问题解决了,如下图

参考链接:

都可以,主要是加了--no-reload,修改代码后就不会自动重载了。所以习惯显式传参,放进环境配置就是隐式传参,怕有一天忘了坑了自己。