繁体   English   中英

QObject :: killTimers错误QThread PyQt

[英]QObject::killTimers error QThread PyQt

我有一个正在编写的脚本,用于通过JSON / XML API从图像板批量下载图像。 以前,它纯粹是CLI,但最近我一直试图在PyQt中构建UI,但取得了很大的成功,但有一个问题:线程阻塞问题,在脚本中实际调用工作线程时GUI不响应。 因此,我试图从threading.Thread切换到QThread,以使其易于管理(通过发出threadFinished SIGNAL更新我的GUI),但是我似乎无法正确设置它。 每当我运行脚本时,线程就会过早死亡。 我在Windows上运行,在Python 2.7.2上使用PyQt4。

经过更多研究,我认为问题出在线程退出,并使用从队列中传递的新元组创建新线程。 我在网上可以找到的所有结果都表明它与应用程序未完全退出有关。

Exception KeyError: KeyError(1188,) in <module 'threading' from 'C:\Python27\lib\threading.pyc'> ignored
QObject::killTimers: timers cannot be stopped from another thread

这是我收到的输出。

有问题的直接代码:

md5_queue是要由Url_Download()填充的md5sum / filename空字典的队列,是文件名/ url的队列元组

在crawler.py中:

num_conn = int(max_threads)
threads = []
for download in range(num_conn):
    t = Url_Download(queue, md5_queue)
    t.start()
    threads.append(t)

从functions.py(我知道这个名字很差)的Url_Download()类:

class Url_Download(QThread):

    file_finished = pyqtSignal(QString, int, name="fileFinished")

    def __init__(self, dl_queue, md5_queue):
        self.dl_queue = dl_queue
        self.md5_queue = md5_queue
        QThread.__init__(self)
    def run(self):
        while 1:
            try:
                count = 0
                file_url, file_path, md5 = self.dl_queue.get_nowait()
                file_extension = str(file_url)[-4:]
                file_name = md5 + file_extension
                while count < 3:
                    count +=1
                    fetch_url(file_url, file_path, md5)
                    if md5 == hash_sum(file_path):
                       self.md5_queue.put_nowait((md5, file_name))
                       self.file_finished.emit("Test!", 10)
                       break

                if count > 3:
                    print 'File failed to download, {} might be corrupt.'.format(file_name)       
                qsize = self.dl_queue.qsize()
                if qsize > 0:
                    print 'Count Remaining: ', qsize
            except Queue.Empty:
                raise SystemExit 
            except:
                traceback.print_exc(file=sys.stderr)
                sys.stderr.flush()

并从GUI.py中,插槽连接:

self.connect(self, SIGNAL("fileFinished(QString, int)"), self.handle_test, Qt.QueuedConnection)

代码的Git(测试分支): https : //github.com/CirnoIsTheStrongest/BiriCrawler/tree/testing

请注意,这是我第一次尝试编码任何东西。 如果有问题,请告诉我

感谢Avaris的帮助,我修复了连接问题,使其指向我的Url_Download()实例。 发生的问题显然没有在窗口上充分显示。 在我的linux VM上,我得到了这个错误:

QThread: Destroyed while thread is still running
QThread: Destroyed while thread is still running
QThread: Destroyed while thread is still running
QThread: Destroyed while thread is still running
Segmentation fault

因此,问题仍然是由我的GUI引起的(我相信),因为我不等待线程在终止之前完成任务。 在GUI.py中引用我的线程对象后,该错误不再发生。 我最终也可以从线程类中向GUI发送信号。 对于那些希望查看涉及的其他更改的人,可以在此处找到完整的代码更改: Github页面,测试分支

在Crawler.py中


    threads = []
    for download in range(self.num_of_threads):
        t = Url_Download(self.dl_queue, self.md5_queue, is_cli=True)
        t.start()
        threads.append(t)

    for thread in threads:
        thread.wait()

在GUI.py中


   main = Crawler(gui_tags, gui_limit, gui_page, gui_booru, gui_savepath, gui_partype, gui_rating, max_threads)
    self.threads = main.start_threads()
    for thread in self.threads:
        self.connect(thread, SIGNAL("fileFinished(QString, int)"), self.onFileFinished, Qt.QueuedConnection)
        self.connect(thread, SIGNAL("allFinished()"), self.onAllFilesFinished, Qt.QueuedConnection)

在functions.py中


class Url_Download(QThread):

    file_finished = pyqtSignal(QString, int, name="fileFinished")        

    def __init__(self, dl_queue, md5_queue, is_cli=False, parent=None):
        QThread.__init__(self, parent)
        self.exiting = False
        self.dl_queue = dl_queue
        self.md5_queue = md5_queue
        self.is_cli = is_cli

    def __del__(self):
        self.exiting = True

     def run(self):
        while not self.exiting:
            try:
                count = 0
                file_url, file_path, md5 = self.dl_queue.get_nowait()
                file_extension = str(file_url)[-4:]
                file_name = md5 + file_extension
                while count < 3:
                    count +=1
                    fetch_url(file_url, file_path, md5)
                    if md5 == hash_sum(file_path):
                       self.md5_queue.put_nowait((md5, file_name))
                       self.file_finished.emit("Test!", 10)
                       break

                if self.is_cli:       
                    if count >= 3:
                        print 'File failed to download, {} might be corrupt.'.format(file_name)       
                    qsize = self.dl_queue.qsize()
                    if qsize > 0:
                        print 'Count Remaining: ', qsize
            except Queue.Empty:
                self.__del__()
            except:
                traceback.print_exc(file=sys.stderr)
                sys.stderr.flush()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM