【问题标题】:QObject::killTimers error QThread PyQtQObject::killTimers 错误 QThread PyQt
【发布时间】:2011-12-27 08:56:06
【问题描述】:

我正在编写一个脚本,用于通过 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/文件名的空字典的队列 queue 是文件名/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

请注意,这是我第一次尝试编写任何代码。如果这是个问题,请告诉我

【问题讨论】:

  • 我无法解决您面临的问题,但 connect 行是错误的。您正在连接QMainWindowfileFinished 信号,该信号不存在。

标签: python windows multithreading pyqt


【解决方案1】:

感谢 Avaris 的帮助,我将连接固定为指向我的 Url_Download() 实例。显然发生的问题在 Windows 上显示得非常不充分。在我的 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 page, testing branch

在 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()

【讨论】:

    猜你喜欢
    • 2013-05-21
    • 1970-01-01
    • 2023-02-05
    • 1970-01-01
    • 1970-01-01
    • 2015-08-13
    • 2015-09-08
    • 2014-03-25
    • 2014-09-05
    相关资源
    最近更新 更多