QObject::killTimers错误QThread PyQ

2024-04-25 18:53:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个脚本,通过JSON/xmlapi从imageboard批量下载图像。以前,它是纯粹的CLI,但最近我尝试用PyQt构建UI,取得了很大的成功,但有一个问题:线程阻塞问题,在脚本中实际调用工作线程时没有响应的GUI。所以,我试着从线程。线程为了使它更易于管理(通过发出threadFinished信号来更新我的GUI),但我似乎无法正确设置它。每当我运行脚本时,线程都会过早死亡。我在windows上运行,PyQt4运行在python2.7.2上。在

经过进一步的研究,我认为问题在于一个线程退出了,并创建了一个新线程,同时从队列中传递了一个新的元组。我能在网上找到的所有结果都指向它是关于应用程序没有干净地退出。在

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空dict的队列 queue是文件名/url的队列元组

在爬虫.py公司名称:

^{pr2}$

从函数.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()

从图形用户界面.py,插槽连接:

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

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

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


Tags: namepyself脚本url队列queuedownload
1条回答
网友
1楼 · 发布于 2024-04-25 18:53:07

感谢Avaris的帮助,我修复了指向我的Url_Download()实例的连接点。显然发生的问题在windows上显示得很不充分。在我的linux虚拟机上,我得到了以下错误:

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没有等待线程在它们被终止之前完成它们的任务。在中引用my threads对象之后图形用户界面.py,错误不再发生。我终于可以从线程类中向GUI发送信号了。对于那些想要查看其他相关更改的人,可以在这里找到完整的代码更改:Github page, testing branch

在爬虫.py在


^{pr2}$

在图形用户界面.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)

在函数.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()

相关问题 更多 >