gpt4 book ai didi

multithreading - Flask 与 PyQt5 - Flask 控制程序流程?

转载 作者:行者123 更新时间:2023-12-03 13:16:52 24 4
gpt4 key购买 nike

Flask 似乎阻止 PyQt5 UI 更新。

相应的代码适用于 PyQt5 或 Flask - 但不能一起使用。我知道这可能与线程的设置方式有关。

任何帮助将不胜感激。 TIA。

`

import sys
import serial
import threading

from PyQt5.QtWidgets import QWidget, QLabel, QApplication
from flask import Flask, render_template, request, redirect, url_for

app1 = Flask(__name__)

ser = serial.Serial ("/dev/ttyS0", 57600,timeout=3) #Open port with baud rate

count=0
temp = []

class Example(QWidget):

def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
global count
count = 1

self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('PyQt5 vs Flask')
self.lbl1 = QLabel('Count '+str(count), self)
self.lbl1.move(100, 50)
self.show()

threading.Timer(5,self.refresh).start()

def refresh(self):
global count
count +=1
print("UI ",count)

self.lbl1.setText('Count'+str(count))
threading.Timer(5,self.refresh).start()

def get_uart():
global temp
if ser.inWaiting()>0:
temp =[str(float(x.decode('utf-8'))) for x in ser.read_until().split(b',')]
print(temp)
threading.Timer(1,get_uart).start()

@app1.route("/")
def index():
global temp
templateData = {'temp1' : temp[1] ,'temp2' : temp[2]}
return render_template('index.html',**templateData)


if __name__ == "__main__":
app = QApplication(sys.argv)
pyqt5 = Example()

threading.Timer(1,get_uart).start()
ser.flushInput()

#app1.run(host='0.0.0.0',threaded=True, port=5000) # ,debug=True)

sys.exit(app.exec_())

`

需要有一个UI来控制要在网站上显示的数据分析。

最佳答案

[已解决]

所有 Flask 参数都可以定义为:

port = int(os.environ.get('PORT', local_port))
kwargs = {'host': '127.0.0.1', 'port': port , 'threaded' : True, 'use_reloader': False, 'debug':False}
threading.Thread(target=app.run, daemon = True, kwargs=kwargs).start()

Flask 不会阻塞并使用 kwargs 中定义的参数运行。

关于multithreading - Flask 与 PyQt5 - Flask 控制程序流程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57031279/

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