【问题标题】:How to avoid freeze loop with threads如何避免线程冻结循环
【发布时间】:2021-02-24 11:28:37
【问题描述】:

我尝试创建 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.x pyqt5


    【解决方案1】:

    作为可能的选项之一,使用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_())
    

    【讨论】:

      【解决方案2】:

      你可以在一个线程中运行not to UI相关函数:

      x = threading.Thread(target=self.checkBulkStatus)
      x.start()
      

      别忘了在某个时候加入。

      x.join()
      

      【讨论】:

      • 你能用我简短的例子告诉我如何实现这个吗?
      猜你喜欢
      • 2020-03-02
      • 2012-06-27
      • 2019-09-04
      • 1970-01-01
      • 1970-01-01
      • 2015-12-11
      • 2018-04-15
      • 1970-01-01
      • 2017-07-14
      相关资源
      最近更新 更多