python-3.x - 如何避免线程冻结循环
问题描述
我尝试创建 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"]
如何运行不同的线程以并行运行。这是可能的?
解决方案
作为可能的选项之一,使用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_())
推荐阅读
- pyspark - PYSPARK:查找不包括前 1 个百分位数据的变量的平均值
- amazon-web-services - AWS Amplify 在创建 DynamoDB 表后创建全局二级索引
- python - 如何从具有多行的文本文件创建整数列表?
- gatsby - 盖茨比网站的 Netlify 构建失败。图片
- typeorm - TypeORM 订阅者 AfterInsert 找不到记录
- python - 我需要帮助为我正在制作的程序制作一个小窗口。它似乎不起作用
- awk - 将花括号块放入 gradle 文件
- graphql - 如何在 Shopify 上使用退款创建 graphQL?
- python - 从“2020-06-28T06:00:18.166286Z”到“%Y-%m-%dT%H:%M:%SZ”给出错误格式 - 为什么?
- php - 无法登录到 Wordpress 管理面板