gpt4 book ai didi

python-3.x - 如何在线程中运行 Flask SocketIO?

转载 作者:行者123 更新时间:2023-12-02 22:45:02 24 4
gpt4 key购买 nike

我正在尝试使用 Python 3 在线程中运行 Flask SocketIO,但无法让它工作。

我的代码不会在 while 循环中继续。

如何在线程中运行它?

import threading
from flask import Flask, render_template, request, redirect, url_for
from flask_socketio import SocketIO

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'


#turn the flask app into a socketio app
socketio = SocketIO(app)


@app.route("/")
def index():
return render_template('index.html', **templateData)

if __name__ == "__main__":
threading.Thread(target=socketio.run(app),kwargs=dict(debug=False, use_reloader=False,host='0.0.0.0', port=5000)).start()
sleep(2)

while True:
try:
Print("Hello I'm in a while loop")

except KeyboardInterrupt:
sys.exit()

最佳答案

您应该将 socketio.run 作为 target 传递,并将 app 作为参数传递

threading.Thread(
target=socketio.run,
args=(app,),
kwargs=dict(debug=False, use_reloader=False,host='0.0.0.0', port=5000)
).start()

您似乎还忘记从时间导休眠眠

from time import sleep

另请注意,templateData 未在代码中定义

关于python-3.x - 如何在线程中运行 Flask SocketIO?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57879090/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com