如何在Qthread(pyqt5)中使用无限循环时解决问题GUI冻结

时间:2019-05-07 14:05:24

标签: python multithreading qthread

我正在用python开发应用程序,功能之一是例如每5秒在文本浏览器中显示ftp接收的文件。用于此目的的qthread会阻止窗口正常工作。

class mainwindow(QMainWindow, Ui_form):
    def __init__(self, parent=None):

        super(mainwindow, self).__init__(parent)
        self.setupUi(self)
        self.show()

        # thread
        self.thread = QThread()
        self.worker = Worker(self)
        self.worker.str_signal.connect(self.message)
        self.worker.moveToThread(self.thread)
        self.thread.started.connect(self.worker.do_work)
        self.thread.finished.connect(self.thread.deleteLater)
        self.worker.str_signal.connect(self.message)
        self.thread.start()

    def message(self, info):
        print(info)



class Worker(QObject):
    str_signal = pyqtSignal(str)

    def __init__(self, parent=None):
        QObject.__init__(self, parent=parent)

    def do_work(self):
        while True:
            j = 0
            ftp = FTP('')
            ftp.connect('XXXXXX', XXXX)
            ftp.login(user='user', passwd='XXXX')
            ftp.cwd("/FTM/Simulateur/1.MPTC_ACK")
            files = ftp.nlst()
            while j < len(files):
                timestamp = ftp.voidcmd("MDTM " + files[j])[4:].strip()
                time = parser.parse(timestamp)
                self.time_dic = str(time)
                self.tab_file = files[j]
                os.chdir("/Users/XXXX/Desktop/eleclink/Fichier_i/1.MPTC_ACK")
                fhandle = open(files[j], 'wb')
                ftp.retrbinary("RETR " + str(self.tab_file), fhandle.write)
                fhandle.close()
                ftp.delete(self.tab_file)
                info = self.m = self.time_dic + " Reception du fichier " + self.tab_file + " réussi\n"
                j = j + 1
                self.str_signal.emit(info)
            QThread.sleep(5)

现在,我尝试在控制台中打印文件,两次输入文件名,同时窗口仍然冻结。

0 个答案:

没有答案