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

标签 multithreading flask pyqt5

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/

相关文章:

python - Sqlite3 内存中多线程问题 - 阻塞的线程立即失败

安卓设计 : How to run 3 different threads in Background for monitoring 3 different devices

python - WebApp 只需要第一次做一些事情,该逻辑放在哪里? Flask 或 django

python - PyQt5 unicode 翻译 : pylupdate5 outputs escape sequences

python - PYQT5:窗口没有获得焦点

python - 从打开的文件中获取文件名,而不是文件路径

c# - 是否可以为使用 Thread 类创建的线程提供回调

javascript - 如何访问 bootstrap-slider 的值(使用 Flask)

python - Flask - 存储模板中的下拉值

multithreading - 事件/信号/等待句柄的操作系统实现