首页 > 解决方案 > 如何避免线程冻结循环

问题描述

我尝试创建 GUI Api。首先,我在控制台中仅使用打印信息构建 python 脚本。

所以我想将应用程序重新构建为具有接口的应用程序。我决定使用 PyQt5

像这样: 在此处输入图像描述

到(第一眼): 在此处输入图像描述

我遇到了循环 While 的问题。应用程序在运行时冻结

我准备了一个简短的脚本来模拟这个问题。主程序看起来不同

import sys
from PyQt5.QtWidgets import *
from PyQt5 import QtWidgets
from termcolor import colored
import time

class App(QMainWindow):
   def __init__(self):
       super().__init__()
       self.title = 'API NORD'
       self.left = 0
       self.top = 0
       self.width = 300
       self.height = 200
       self.setWindowTitle(self.title)
       self.resize(800, 600)
       self.center()
       self.table_widget = MyTableWidget(self)
       self.setCentralWidget(self.table_widget)

       self.show()
    def center(self):
       # geometry of the main window
       qr = self.frameGeometry()

       # center point of screen
       cp = QDesktopWidget().availableGeometry().center()

       # move rectangle's center point to screen's center point
       qr.moveCenter(cp)

    # top left of rectangle becomes top left of window centering it
       self.move(qr.topLeft())


class MyTableWidget(QWidget):

   def __init__(self, parent):
       super(QWidget, self).__init__(parent)

       self.layout = QVBoxLayout(self)
       self.pushButton1 = QPushButton("Run")
       self.layout.addWidget(self.pushButton1)
       self.pushButton1.clicked.connect(self.button2_clicked)
       self.textedit = QtWidgets.QTextEdit(readOnly=True)
       self.layout.addWidget(self.textedit)
       self.textedit.setText("STATUS")



   def onClicked(self):
       radioButton = self.sender()
       if radioButton.isChecked():
           x=0
        # print("Shop is %s" % (radioButton.shop))
           self.Sklep=radioButton.shop
           self.l1.setText(self.Sklep)
       return


   def checkBulkStatus(self):
       Status = "Start"
       x=0
       self.textedit.setText("Start")
       while x < 5:

           print("Aktualny Status:", colored(Status,"yellow"))
           Status="Running"
           self.textedit.append(Status)
           if Status=="FAILED":
               print("Error")
               break
           time.sleep(2.5)
           x+=1

       print("Aktualny Status: ", colored("COMPLETED", "green"))
       self.textedit.setText("COMPLETED")


   def button2_clicked(self):
       self.checkBulkStatus()

if __name__ == '__main__':
   app = QApplication(sys.argv)
   ex = App()
   sys.exit(app.exec_())

在主程序中,我使用 while 检查 GraphQL 中 BULK 请求的状态:

def checkBulkStatus(self):
    self.url = self.auth(self.Sklep)["url_auth"]
    print(self.url)
    Status = "Start"
    self.textedit.setText("Start")
    while Status != "COMPLETED":

    print("Aktualny Status:", colored(Status,"yellow"))
    checking = self.Core.callShopifyGraphQL(self.Core.CheckQuery,self.url)
    result = checking.json()
    Status=result["data"]["currentBulkOperation"]["status"]
    self.textedit.append(Status)
    if Status=="FAILED":
        print(result["data"]["currentBulkOperation"])
        break
    time.sleep(2.5)

print("Aktualny Status: ", colored("COMPLETED", "green"))
URL_bulk=result["data"]["currentBulkOperation"]["url"]

如何运行不同的线程以并行运行。这是可能的?

标签: python-3.xpyqt5

解决方案


作为可能的选项之一,使用QTimer.

该类QTimer提供重复和单次计时器。
更多 https://doc.qt.io/qt-5/qtimer.html

import sys
from PyQt5.QtWidgets import *
from PyQt5 import QtWidgets, QtCore                                       # + QtCore
#from termcolor import colored
#import time


class MyTableWidget(QWidget):
    def __init__(self, parent):
        super(QWidget, self).__init__(parent)

        self.layout = QVBoxLayout(self)
        self.pushButton1 = QPushButton("Run")
        self.layout.addWidget(self.pushButton1)
        self.pushButton1.clicked.connect(self.button2_clicked)
        self.textedit = QtWidgets.QTextEdit(readOnly=True)
        self.layout.addWidget(self.textedit)
        self.textedit.setText("STATUS")
        
        self.timer = QtCore.QTimer(self)                                  # +++
        self.timer.timeout.connect(self.checkBulkStatus)                  # +++
        
    def checkBulkStatus(self):
        _status = f"Running: x={self.x}"
        self.textedit.append(_status)
        if _status == "FAILED":
            self.textedit.append(_status + "FAILED")
            self.timer.stop()
            
        self.x += 1
        if self.x >= 5:
            self.timer.stop()
            self.textedit.append("COMPLETED")

    def button2_clicked(self):
        self._status = "Start"
        self.x = 0
        self.textedit.setText("Start")
        self.timer.start(2500)
        

class App(QMainWindow):
    def __init__(self):
        super().__init__()
        self.title = 'API NORD'
        self.left = 0
        self.top = 0
        self.width = 300
        self.height = 200
        self.setWindowTitle(self.title)
        self.resize(800, 600)
        self.center()
        
        self.table_widget = MyTableWidget(self)
        self.setCentralWidget(self.table_widget)

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        # move rectangle's center point to screen's center point
        qr.moveCenter(cp)
        # top left of rectangle becomes top left of window centering it
        self.move(qr.topLeft())


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    ex.show()
    sys.exit(app.exec_())

在此处输入图像描述


推荐阅读