我有一个应用程序,它与某些API通信以上传数据。
它有ExportDialog,它有ProgressBar。
algorythm如下:
有时,互联网连接可能会丢失,所以我有尝试...除了块,它会捕获请求的ConnectionError。 我的问题是,如果互联网连接丢失,有时会卡住。
让我们在代码中展示它。 的 ExportDialog
class ExportDialog(QtGui.QDialog):
def __init__(self, table, api):
super(ExportDialog, self).__init__()
self.table = table
self.stat = list()
self.value = 0
self.task = WorkThread(self.table)
self.length = len(self.table)
self.api = api
self.initUI()
self.retranslateUI()
self.initActions()
WorkThread类
class WorkThread(QtCore.QThread):
punched = QtCore.pyqtSignal(dict)
def __init__(self, table, index=0):
super(WorkThread, self).__init__()
self.table = table[index:]
def __del__(self):
self.wait()
def run(self):
for i in self.table:
self.punched.emit(i)
time.sleep(1)
def stop(self):
self.terminate()
ExportDialog.initActions
def initActions(self):
self.startButton.clicked.connect(self.changeTask)
self.breakButton.clicked.connect(self.task.stop)
self.breakButton.clicked.connect(self.close)
ExportDialog.changeTask
def changeTask(self):
if self.sender().text() == "Start":
self.startButton.setText(self.tr("Stop"))
self.breakButton.show()
self.setWindowTitle(self.tr("Processing..."))
if self.value > 0:
self.task = WorkThread(self.table, self.value)
self.task.punched.connect(self.onProgress)
self.task.start()
else:
self.task.stop()
self.startButton.setText(self.tr("Start"))
self.breakButton.hide()
ExportDialog.onProgress
def onProgress(self, i):
try:
row = i
word = row.get('word').lower()
context = row.get('context', '')
translate = self.api.get_translate(word)
self.api.add_word(translate['word'],
translate['tword'],
context)
if translate['is_exist']:
result = "Exist"
else:
if translate['tword'] == "No translation":
result = "No translation"
else:
result = "New"
self.api.substractMeatballs()
meatballs = "Meatballs: {}".format(self.api.meatballs)
self.meatballs_label.setText(meatballs)
self.stat.append({"word": word,
"result": result,
"tword": translate['tword']})
except ConnectionError:
self.startButton.click()
# this is where WarningDialog should be shown
# but in my case it happens 50/50
warning = WarningDialog("No Internet Connection")
warning.exec_()
return
api - API实例,其中包含:
__
如果我手动点击停止然后开始,一切顺利。
如果我手动放弃互联网连接,就会发生奇怪的事情。
我哪里错了?
答案 0 :(得分:1)
每次调用changeTask
时,task.punched
都会再次连接onProgress
个广告位。这意味着将在每个task.punched
信号上多次调用插槽。
如果在onProgress
)广告位内发生错误,则会点击startButton
。一开始一切正常:按钮的文本是“停止”,因此它只是停止任务。但是插槽多次发射,对吧?第二次是不同的:按钮的文字现在是“开始”,它再次连接task.punched
和onProgress
并启动任务,最终会调用onProgress
...见面无限递归。
def changeTask(self):
if self.sender().text() == "Start":
[...]
self.task.punched.connect(self.onProgress) # <--
self.task.start()
else:
[...]
def onProgress(self, i):
try:
[...]
except ConnectionError:
self.startButton.click() # <--
warning = QtGui.QDialog()
warning.exec_()
return
сonclusion:
self.task.punched.connect(self.onProgress)
从changeTask
移至__init__
)答案 1 :(得分:0)
稍微更改了代码,现在它可以正常工作
<强> WorkThread 强>
def __init__(self):
super(WorkThread, self).__init__()
# new method
def getData(self, table, index=0):
self.table = table[index:]
所以,在ExportDialog init
中self.task = WorkThread()
self.task.getData(table)
打孔已发送至 initActions
def initActions(self):
self.startButton.clicked.connect(self.changeTask)
self.breakButton.clicked.connect(self.task.stop)
self.breakButton.clicked.connect(self.close)
self.task.punched.connect(self.onProgress) # <-- Here we go!
和changeTask
def changeTask(self):
if self.sender().text() == "Start":
self.startButton.setText(self.tr("Stop"))
self.breakButton.show()
self.setWindowTitle(self.tr("Processing..."))
if self.value > 0:
self.task.getData(self.table, self.value)
self.task.start()
else:
self.task.stop()
self.startButton.setText(self.tr("Start"))
self.breakButton.hide()
感谢 Alexander Lutsenko 给我正确的方式