Tkinter使用多线程时的线程阻塞问题

PDF解密小工具

项目场景

PDF破解小工具
该工具使用了pypdf2库,它的主要功能提供两个功能,一个是对于能打开但是限制编辑的PDF文件,移除其密码,使其能够编辑和打印;另一个是针对设置了打开密码的PDF,选择密码字典库进行暴力破解,由于密码字典很大,读取PDF又是I/O密集操作,单线程遍历时耗时太久,所以多开辟几个线程进行遍历,使得速度大大提升。

  1. 通过生成器将一个密码字典分成块迭代器
  2. 使用多线程分别遍历不同的生成器
  3. 使用队列传递解密进度信息,并在GUI更新解密进度
  4. 解密中途可以随时点击取消

问题描述

使用了线程池的方式来管理多线程,因为知道tkinter是线程不安全的,所以进度条的更新使用root.after()来监控并更新,真正操作PDF的工作都在excutor线程池中进行,而进度条的更新是在主线程中,按道理两者应该是不会发生阻塞的,而且在程序运行时会发现程序一旦运行,是无法取消执行的,整个主线程都会处于阻塞状态。

...
# 开启线程,开始破解 
def start_deception_pdf_thread(root, filename, progress, progress_label,crack_dic=None):

    # 创建一个线程安全的队列来传递进度信息 
    progress_queue = queue.Queue() 
    # 重置终止标志
    thread_staus.set_state(True) 
    print(crack_dic) 
    if crack_dic == None or crack_dic == "": 
        deception_pdf(root,filename, progress,progress_label)

    else:
        # 获取字典库总行数,并传到函数里面,用于显示进度条 
        count_lines = count_lines_in_file(crack_dic) 
        chunks = chunks_generator(crack_dic,1000) 
        # 使用 ThreadPoolExecutor 管理线程池 
        with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:

            # 将生成器的每一行作为任务提交到线程池
            futures = [executor.submit(deception_pdf,root,filename, progress_queue,count_lines,chunk)for chunk in chunks]
            # 等待所有任务完成(可选)使用这个会导致在遍历完后,程序卡死
            #concurrent.futures.wait(futures)
            # 确保所有进度更新都已完成
            progress_queue.join()
# 更新进度条的方法
def check_progress(root,progress, progress_label, progress_queue, total_tasks):
    current_state = thread_staus.get_state()
    if current_state:
        if not progress_queue.empty():
            # 阻塞调用,如果队列为空,则等待
            completed_work = progress_queue.get(block=True)
            current_value = progress['value'] + completed_work
            progress['value'] = current_value
            progress_label.config(text=f"Progress: {current_value / total_tasks * 100:.1f}%")
            print("刷新进度条{}".format(current_value))
            if current_value > total_tasks:
                current_state = False
                thread_staus.set_state(False)
            root.update_idletasks()
    else:
        root.update_idletasks()
# 创建主函数
def main():
...
	root.after(20,lambda:check_progress(root,progress, progress_label, progress_queue,count_lines))
...

原因分析:

实际上这里很容易造成误解(我就是),以为excutor线程池跟主线程是分开的,所以用了很长时间才搞清楚,也见识了各种各样奇葩的情况,比如:进度条每次在所有程序执行完毕后才开始动、程序运行结束后就会卡死、运行时不让取消等等。

在这里需要明确的一点是,开辟线程池的操作还是在主线程中,由于我们使用了生成器,生成器是每访问一次给你一个,所以多线程的情况下,访问会很频繁,导致主线程阻塞,而如果让GUI的更新跟线程池的操作互不干扰,就应该将开辟线程池的操作单独开一条线程,否则的话还是会跟主线程竞争资源,导致程序运行后产生卡死、无响应等情况。


解决方案:

再封装一个函数,将开辟线程池的操作单独再开一条线程,后台去做访问生成器、建立线程池等操作:

# 更新进度条的方法 
...
def check_progress(root,progress, progress_label, info_label, progress_queue, info_queue,input_crack_dic_var):
    global complete_num 
    global total_tasks 
    global comput_status 
    global progress_status 
    # 实时监控选中的字典路径 
    crack_dir = input_crack_dic_var.get() 
    try:
        current_status = thread_staus
        if current_status:
            # 如果用户没有选择密码字典,就什么也不做
            if crack_dir == None or crack_dir == "":
                pass
            else:
                # 获取用户选择的字典中总共有多少条密码,这个只会获取一次
                if comput_status:
                    total_tasks = count_lines_in_file(crack_dir)
                    comput_status = False
                progress['max'] = total_tasks
                # print("total_tasks:{}".format(total_tasks))
                # print("共需遍历{}条数据".format(total_tasks))
                # 只有当队列中有数据时,才读取并更新进度条
                if not progress_queue.empty():
                    # print("complete_num==>{}".format(complete_num))
                    if complete_num < total_tasks:
                        completed_work = progress_queue.get(block=True, timeout=5)
                        complete_num = complete_num + completed_work
                        progress['value'] = complete_num
                        progress_label['text'] = "已完成:{:.1f}%".format((progress["value"]/total_tasks)*100)
                    else:
                        info_label['text'] = "遍历了{}条密码,未找到匹配的密码,请换个密码字典试一试吧!".format(total_tasks)
                        progress_status = False
                    root.update_idletasks()
                    progress_queue.task_done()
            if not info_queue.empty():
                info_label['text'] = info_queue.get(block=True, timeout=5)
    except Exception as e:
        info_label['text'] = "出现了错误,请重新运行程序"
        pass
    finally:
        if progress_status:
            root.after(2000,lambda:check_progress(root,progress, progress_label, info_label,progress_queue,info_queue,input_crack_dic_var))
# 开启线程,开始破解
def start_deception_pdf_thread(root, filename,progress_queue,info_queue,count_lines,chunks):
        # 使用 ThreadPoolExecutor 管理线程池
        with concurrent.futures.ThreadPoolExecutor(max_workers=6) as executor:
            # 将生成器的每一行作为任务提交到线程池  
            futures = [executor.submit(deception_pdf,root,filename, progress_queue,info_queue,count_lines,chunk)for chunk in chunks]
            # 等待所有任务完成(可选)使用这个会导致在遍历完后,程序卡死   
            #concurrent.futures.wait(futures)   
            # 确保所有进度更新都已完成   
            progress_queue.join()   

def start_back_threads(root, filename,progress_queue,info_queue,crack_dic=None):
    # 重置终止标志
    thread_staus.set_state(True)   
    if crack_dic == None or crack_dic == "":   
        deception_pdf(root,filename,progress_queue,info_queue)
    else:
        # 获取字典库总行数,并传到函数里面,用于显示进度条   
        count_lines = count_lines_in_file(crack_dic)   
        chunks = chunks_generator(crack_dic,1000)   
        decryp_pdf_thread = threading.Thread(target=start_deception_pdf_thread,args=(root, filename, progress_queue,info_queue,count_lines,chunks))
        decryp_pdf_thread.start()   
...

项目源码地址

  1. Python-PDF文件密码移除小工具
  2. PDFDecryper
### Tkinter多线程使用方法及注意事项 在 Tkinter 应用程序中,多线程技术可以用于处理耗任务(如网络请求、文件操作等),从而避免 GUI 界面因阻塞而变得无响应。以下是关于如何在 Tkinter使用多线程的详细说明以及需要注意的关键点。 #### 多线程的基本实现 为了在 Tkinter 中实现多线程功能,通常会使用 Python 的 `threading` 模块。以下是一个简单的示例,展示如何通过多线程执行耗任务,同保持 GUI 界面的响应性: ```python import tkinter as tk from tkinter import messagebox import threading import time class Application(tk.Tk): def __init__(self): super().__init__() self.title("Tkinter 多线程示例") self.geometry("300x150") self.label = tk.Label(self, text="点击按钮启动后台任务", font=("Arial", 12)) self.label.pack(pady=20) self.button = tk.Button(self, text="启动任务", command=self.start_task) self.button.pack() def start_task(self): # 禁用按钮以防止重复点击 self.button.config(state=tk.DISABLED) # 创建并启动新线程 thread = threading.Thread(target=self.long_running_task) thread.start() def long_running_task(self): for i in range(5, 0, -1): self.label.config(text=f"倒计: {i} 秒") time.sleep(1) # 更新完成信息 self.label.config(text="任务已完成") messagebox.showinfo("提示", "后台任务已成功完成!") # 启用按钮以便再次点击 self.button.config(state=tk.NORMAL) if __name__ == "__main__": app = Application() app.mainloop() ``` #### 注意事项 1. **主线程与 GUI 更新** Tkinter 的 GUI 元素必须在主线程中更新。如果尝试从子线程直接修改 GUI 元素,可能会导致未定义行为或崩溃。因此,在多线程环境中,应确保所有与 GUI 相关的操作都在主线程中完成[^1]。 2. **线程安全** 在多线程应用中,需特别注意线程安全问题。例如,多个线程可能同访问共享资源(如全局变量)。为避免竞争条件,可以使用锁(`threading.Lock`)来同步对共享资源的访问[^1]。 3. **避免阻塞线程** 耗操作(如网络请求、文件读写等)应始终在子线程中执行,以防止阻塞 Tkinter 的事件循环。事件循环被阻塞会导致界面无法响应用户输入。 4. **线程生命周期管理** 应合理管理线程的生命周期,避免创建过多线程或让线程无限期运行。对于需要频繁执行的任务,可以考虑使用线程池(`concurrent.futures.ThreadPoolExecutor`)[^3]。 #### 使用线程池优化多线程任务 当应用程序需要频繁创建销毁线程,可以使用线程池来提高性能资源利用率。以下是一个使用 `ThreadPoolExecutor` 的示例: ```python from concurrent.futures import ThreadPoolExecutor class ApplicationWithThreadPool(tk.Tk): def __init__(self): super().__init__() self.title("Tkinter 线程池示例") self.geometry("300x150") self.label = tk.Label(self, text="点击按钮启动后台任务", font=("Arial", 12)) self.label.pack(pady=20) self.button = tk.Button(self, text="启动任务", command=self.start_task_with_pool) self.button.pack() # 创建线程池 self.executor = ThreadPoolExecutor(max_workers=5) def start_task_with_pool(self): self.button.config(state=tk.DISABLED) self.executor.submit(self.long_running_task) def long_running_task(self): for i in range(5, 0, -1): self.label.config(text=f"倒计: {i} 秒") time.sleep(1) self.label.config(text="任务已完成") messagebox.showinfo("提示", "后台任务已成功完成!") self.button.config(state=tk.NORMAL) if __name__ == "__main__": app = ApplicationWithThreadPool() app.mainloop() ``` #### 最佳实践 - **任务分离**:将耗任务与 GUI 控制逻辑分离,确保主线程专注于处理用户交互。 - **异常处理**:在子线程中添加适当的异常捕获机制,防止因未处理的异常导致整个程序崩溃。 - **资源释放**:在应用程序关闭,确保所有线程正确终止,并释放相关资源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凉拌糖醋鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值