简体   繁体   中英

Tkinter - multithreading or multiprocessing in for loop

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM