tkinter-多线程或多处理以进行循环

发布于 2025-02-13 08:23:58 字数 2518 浏览 0 评论 0原文

我想优化我的程序。简而言之,它用于解压缩巨大的tar.gz存档(5 GB+)或根据输入从中提取一些文件(约20K)。

我现在面临的主要问题是窗口没有响应,但我确实需要它才能正常工作。我已经阅读了有关多处理和多线程的信息,但不确定如何将其应用于我的代码上。

你能帮我吗?

这是我解开拉链的2个功能

def unzip_tar_gz(self, ARES_tar_gz):
        row             = 22 #used for GUI
        self.debug_output("Unzip", row, "Start: ")
        tarfile_ARES    = tarfile.open(ARES_tar_gz)
        
        #counters for progress bar
        self.counter    = 0
        self.maxcount   = 1200000
        
        #extract
        for member in tarfile_ARES.getmembers():
            self.update_idletasks()
            member.name = os.path.basename(member.name)
            result      = re.search("(.*).xml", member.name)
            res         = result.group(1)
            r           = res[:-1]
            num         = int(r)
            tarfile_ARES.extract(member, self.get_folder(num))
            self.counter+=1
            
            #debug
            if self.counter%100000 == 0:
                row += 1
                self.debug_output("extract 100k", row, "Success")

        #after extract, closing
        showinfo("Success", message="The execution was successfully completed.")
        self.pb.stop()
        self.destroy()

def unzip_changes(self, delta_file_arr, inputARES):
    row            = 22 #used for GUI

    #debug
    self.debug_output("Unzip changes", row, "Start: ")

    tarfile_ARES = tarfile.open(inputARES)
    time_not_found = date.today().strftime("%Y-%m-%d")
    file_not_found = open(self.logs_path + time_not_found + '.log', 'w')

    #counters for progress bar
    self.counter    = 0
    self.maxcount   = 22000

    #extract
    for name in delta_file_arr:
        self.update_idletasks()
        self.counter +=1
        try:
            member      = tarfile_ARES.getmember(name)
            member.name = os.path.basename(member.name)
            result      = re.search("(.*).xml", member.name)
            res         = result.group(1)
            r           = res[:-1]
            num         = int(r)
            tarfile_ARES.extract(member, self.get_folder(num))
        except:
            result         = re.search("./VYSTUP/DATA/(.*).xml", name)
            res            = result.group(1)
            file_not_found.write(res + "\n")
            continue
    #after extract, closing
    self.pb.stop()
    showinfo("Success", message="The execution was successfully completed.")
    file_not_found.close()
    self.destroy()

I would like to optimize my program. In short, it is used to unzip huge tar.gz archive (5 gb+) or extract some file from it (approx. 20k) based on input.

The main problem I am facing right now is that window is not responding, but I really need it to work properly. I have read about multiprocessing and multithreading, but not sure how to apply it on my code.

Could you, please, help me with it?

Here are 2 my functions for unziping:

def unzip_tar_gz(self, ARES_tar_gz):
        row             = 22 #used for GUI
        self.debug_output("Unzip", row, "Start: ")
        tarfile_ARES    = tarfile.open(ARES_tar_gz)
        
        #counters for progress bar
        self.counter    = 0
        self.maxcount   = 1200000
        
        #extract
        for member in tarfile_ARES.getmembers():
            self.update_idletasks()
            member.name = os.path.basename(member.name)
            result      = re.search("(.*).xml", member.name)
            res         = result.group(1)
            r           = res[:-1]
            num         = int(r)
            tarfile_ARES.extract(member, self.get_folder(num))
            self.counter+=1
            
            #debug
            if self.counter%100000 == 0:
                row += 1
                self.debug_output("extract 100k", row, "Success")

        #after extract, closing
        showinfo("Success", message="The execution was successfully completed.")
        self.pb.stop()
        self.destroy()

and

def unzip_changes(self, delta_file_arr, inputARES):
    row            = 22 #used for GUI

    #debug
    self.debug_output("Unzip changes", row, "Start: ")

    tarfile_ARES = tarfile.open(inputARES)
    time_not_found = date.today().strftime("%Y-%m-%d")
    file_not_found = open(self.logs_path + time_not_found + '.log', 'w')

    #counters for progress bar
    self.counter    = 0
    self.maxcount   = 22000

    #extract
    for name in delta_file_arr:
        self.update_idletasks()
        self.counter +=1
        try:
            member      = tarfile_ARES.getmember(name)
            member.name = os.path.basename(member.name)
            result      = re.search("(.*).xml", member.name)
            res         = result.group(1)
            r           = res[:-1]
            num         = int(r)
            tarfile_ARES.extract(member, self.get_folder(num))
        except:
            result         = re.search("./VYSTUP/DATA/(.*).xml", name)
            res            = result.group(1)
            file_not_found.write(res + "\n")
            continue
    #after extract, closing
    self.pb.stop()
    showinfo("Success", message="The execution was successfully completed.")
    file_not_found.close()
    self.destroy()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哆啦不做梦 2025-02-20 08:23:58

您应该为要运行的每个功能使用线程,因为如果不这样做,则TKINTER窗口将冻结,直到功能完成为止。因此,请使用:

import threading

此处具有按钮的示例:

Button = tk.Button(text='Thread',command=lambda: threading.Thread(target=my_function).start())

如果使用类:

self.Button = tk.Button(self, text='Thread',command=lambda: threading.Thread(target=self.my_function).start())

You should use a thread for each function you want run because if you don't, the tkinter window will freeze until the function finish for example. So use:

import threading

Here an example with a button:

Button = tk.Button(text='Thread',command=lambda: threading.Thread(target=my_function).start())

if you use class:

self.Button = tk.Button(self, text='Thread',command=lambda: threading.Thread(target=self.my_function).start())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文