tkinter-多线程或多处理以进行循环
我想优化我的程序。简而言之,它用于解压缩巨大的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该为要运行的每个功能使用线程,因为如果不这样做,则TKINTER窗口将冻结,直到功能完成为止。因此,请使用:
此处具有按钮的示例:
如果使用类:
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:
Here an example with a button:
if you use class: