Skip to main content
 首页 » 编程设计

python之暂停 QThread 的运行方法来测试它

2025年05月04日28zhoujg

我像这样创建一个 Worker 对象:

from PyQt4 import QtSql, QtCore 
from requests_futures.sessions import FuturesSession 
 
 
class Worker(QtCore.QThread): 
 
    def __init__(self): 
 
        QtCore.QThread.__init__(self) 
 
        self.exiting = False 
        self.list_urls = # Urls coming from somewhere 
 
        # List to store the urls of the pages to request 
        self.list_futures_urls = [] 
 
 
    def __del__(self): 
 
        """Method to destroy the thread properly""" 
 
        self.exiting = True 
        self.wait() 
 
 
    def run(self): 
 
        """Main function. Starts the real business""" 
        print(self.exiting) # -> True !!!!! 
 
        session = FuturesSession(max_workers=10) 
 
        for url in self.list_urls: 
 
            future = session.get(url) 
            future.add_done_callback(completeData) 
            self.list_futures_urls.append(future) 
 
        # HERE !!!!! WHY ? 
        print(self.exiting) # -> True 
        self.exiting = False 
        print(self.exiting) # -> False 
 
        while not self.exiting: 
            self.sleep(20000) 
            # self.wait() 
 
 
    def completeData(self): 
 
        total_futures = self.list_futures_urls 
 
        # Checking if all the futures have started and finished 
        if len(total_futures) !=  len(self.list_urls): 
            return 
        else: 
            states_futures = [] 
            for result in total_futures: 
                states_futures.append(result.done()) 
 
            if False not in states_futures: 
                self.exiting = True 
                print("boum") 

如你所见,我的worker是一个线程,并且启动了一个异步请求Session。所以 run() 方法启动异步 Web 请求,然后完成。在这个阶段,线程通常会发送一个“完成”信号。但是异步的web请求当然没有做完。

在完成所有异步 Web 请求之前,我该怎么做才能阻止“完成”信号?

我尝试在 run() 方法的末尾使用 wait(),但我得到:

QThread::wait: Thread tried to wait on itself 

我也试过使用 sleep(),但没有结果,'finished' 信号仍然发出。

有什么想法吗?

请您参考如下方法:

我无法重现,但我有一些注释可能会有所帮助:

可能发生的是所有异步任务在解释器到达行之前完成:

 while not self.exiting: 

您假设异步调用会给您足够的时间在所有处理完成之前到达 while 语句。

另一方面,由于您使用的是 QtCore.QThread,因此您不需要控制变量来了解何时停止,您可以使用其中一个来停止线程:QThread.exit , QThread.quit或 QThread.terminate ,请注意最后不推荐。

关于 self.exiting 变为 True,这非常罕见,我不得不说,但是如果不执行line self.exiting = True 我建议在那里设置一个断点并查看调用堆栈以跟踪导致该行被执行的调用路径。

另一个想法是设置一个Hardware Breakpoit查看 self.exiting 的值何时更改。

可能的解决方案:

class Worker(QtCore.QThread): 
    # I don't test this code, is just for show 
    # the way to go. 
 
    def __init__(self): 
 
        QtCore.QThread.__init__(self) 
        self.list_urls = # Urls coming from somewhere 
        self.list_futures_urls = [] 
 
    def run(self): 
 
        """Main function. Starts the real business""" 
        session = FuturesSession(max_workers=10) 
 
        for url in self.list_urls: 
            future = session.get(url) 
            future.add_done_callback() 
            self.list_futures_urls.append(future) 
 
        while (True): 
            # You could add some sleep here in order to create a small delay between iterations. 
            # WARNING: You have to aware of the posibility that a result.get() call never returns True, 
            #          in that this code (as well as yours) will run into an infinite loop/thread. 
            results = [result.done() for result in  self.list_futures_urls] 
            if (False not in results): 
                self.exit() 
 
        # Callback removed from this code since the only purpose of  
        # it was to end the thread when all task get done.