Tkinter中线程内的并行循环

时间:2018-09-19 14:07:38

标签: python tkinter parallel-processing python-multithreading joblib

我正在创建一个Tkinter GUI,可以在其中处理多个图像。每个过程都花费大量时间,并涉及使用Parallel函数。显然,它使GUI显得有些僵硬,这就是为什么我想将进程放在单独的线程中以便更新映像列表,并知道哪些映像已被处理以及哪些正在进行中,如图所示:

enter image description here

因此,当按下“过程”按钮时,将执行与此功能相似的功能:

def process_perusat(self):

    paths_check = []

    cnt=0
    for key, value in intvar_dict.items():
        if value.get() > 0:
            src_path = path + "/" + key
            paths_check.append(src_path) #Saves the full path of each image
            cnt=cnt+1    

    if cnt==0:
        alerta_msg("There are no selected images")
    else:
        tkmb.showinfo("Starting process", str(cnt)+" images are going to be processed")

        def proceso():
            cnt=0
            for paths_check_i in paths_check:                    
                name = self.checkbutton_list[cnt].cget("text")
                self.checkbutton_list[cnt].config(text=name + "(procesando)")

                #...
                #The image is divided in "Blocks" and each one
                #will be processed in parallel by the function "slic_patches"
                #...

                R=Parallel(n_jobs=-1)(delayed(slic_patches)(Blocks[i]) for i in range(0,len(Blocks)))

                cnt=cnt+1

        t = threading.Thread(target=proceso, name = 'MainThread')
        t.start()

proceso()函数和线程t允许GUI不冻结。但是,出现此警告:

C:\Users\User\Anaconda3\lib\site-packages\joblib\parallel.py:705: UserWarning: 
Loky-backed parallel loops cannot be nested below threads, setting n_jobs=1

这意味着每个Parallel循环不能使用所有的CPU,并且过程要慢得多。

你有什么解决办法吗?

预先感谢

0 个答案:

没有答案