课堂上的多处理未使用完整的CPU范围

时间:2019-10-30 10:47:54

标签: python python-3.x class multiprocessing

我有一个程序,该程序在启动时会打开一个GUI,该GUI允许用户选择一个原始文件,然后读取该文件,然后调用一个函数来计算一些内容,然后将其保存在一个pickle文件中。 / p>

取决于文件的大小,此过程最多可能需要12个小时或更长时间。 (大量计算) 因此,我的方法是允许程序通过多处理模块在不同的进程中运行计算。但是似乎我没有使用全部的CPU。当我启动第一个进程时,CPU利用率从4-5%上升到大约20%,但是当我启动第二个进程时,它保持在20%左右

代码很大,可以在这里发布,所以我试图尽可能地解释它

class main():
    def __init__(self,master):
            self.processes= {'1': 0, '2' :0, '3' :0,'4':0,'5':0,'6':0}
            self.master = master
            self.master.title("Framework")
            self.master.minsize(1450, 650)
            self.master.maxsize(1450,650)
            style = ttk.Style()
            style.theme_use('clam')
            style.configure('Custom.TButton',background='#94FDFA')
            self.Box4 = Entry(self.master)
            self.text = Text(self.master)
            self.scroll = Scrollbar(self.master, command=self.text.yview)
            self.text.configure(yscrollcommand=self.scroll.set)
            self.text.tag_configure('bold_italics', font=('Verdana', 12, 'bold', 'italic'))
            self.text.place( x = 450, y =80,bordermode="inside", width=300,height=250)
            self.scroll.place(x=750,y=80, height= 250)

            self.text2 = Text(self.master)
            self.scroll2 = Scrollbar(self.master, command=self.text.yview)
            self.text2.configure(yscrollcommand=self.scroll2.set)
            self.text2.tag_configure('bold_italics', font=('Verdana', 12, 'bold', 'italic'))
            self.text2.place( x = 775, y =80,bordermode="inside", width=300,height=250)
            self.scroll2.place(x=1075,y=80, height= 250)            

            self.text3 = Text(self.master)
            self.scroll3 = Scrollbar(self.master, command=self.text.yview)
            self.text3.configure(yscrollcommand=self.scroll3.set)
            self.text3.tag_configure('bold_italics', font=('Verdana', 12, 'bold', 'italic'))
            self.text3.place( x = 1100, y =80,bordermode="inside", width=300,height=250)
            self.scroll3.place(x=1400,y=80, height= 250)     

            self.text4 = Text(self.master)
            self.scroll4 = Scrollbar(self.master, command=self.text.yview)
            self.text4.configure(yscrollcommand=self.scroll4.set)
            self.text4.tag_configure('bold_italics', font=('Verdana', 12, 'bold', 'italic'))
            self.text4.place( x = 450, y =370,bordermode="inside", width=300,height=250)
            self.scroll4.place(x=750,y=370, height= 250)

            self.text5 = Text(self.master)
            self.scroll5 = Scrollbar(self.master, command=self.text.yview)
            self.text5.configure(yscrollcommand=self.scroll5.set)
            self.text5.tag_configure('bold_italics', font=('Verdana', 12, 'bold', 'italic'))
            self.text5.place( x = 775, y =370,bordermode="inside", width=300,height=250)
            self.scroll5.place(x=1075,y=370, height= 250)            

            self.text6 = Text(self.master)
            self.scroll6 = Scrollbar(self.master, command=self.text.yview)
            self.text6.configure(yscrollcommand=self.scroll6.set)
            self.text6.tag_configure('bold_italics', font=('Verdana', 12, 'bold', 'italic'))
            self.text6.place( x = 1100, y =370,bordermode="inside", width=300,height=250)
            self.scroll6.place(x=1400,y=370, height= 250)  
            self.Box4.place( x = 10, y = 10,bordermode="inside", width=820,height=35)
            self.b1 = ttk.Button(self.master,text = "Folder", command=self.select_folder)
            self.b1.place(x=850, y=10)   
            self.b2 = ttk.Button(self.master, text="run", command=self.go)
            self.b2.place(x=20, y=600)
            self.master.geometry('%dx%d+%d+%d' % (950, 400, 20, 20))


    def update(self, tid):
        try:
            if tid == self.processes['2']:
                self.text.insert(END, (self.text1 + '\n'))
                self.text.see("end")
        except IndexError:
            return
        except AttributeError:
            return                
        try:
            if tid == self.processes['1']:
                self.text2.insert(END, (self.text1 + '\n'))
                self.text2.see("end")        
        except IndexError:
            return
        except AttributeError:
            return
        try:
            if tid == self.processes['3']:
                self.text3.insert(END, (self.text1 + '\n'))
                self.text3.see("end")
        except IndexError:
            return
        except AttributeError:
            return                
        try:
            if tid == self.processes['4']:
                self.text4.insert(END, (self.text1 + '\n'))
                self.text4.see("end")        
        except IndexError:
            return
        except AttributeError:
            return
        try:
            if tid == self.processes['5']:
                self.text5.insert(END, (self.text1 + '\n'))
                self.text5.see("end")
        except IndexError:
            return
        except AttributeError:
            return                
        try:
            if tid == self.processes['6']:
                self.text6.insert(END, (self.text1 + '\n'))
                self.text6.see("end")        
        except IndexError:
            return
        except AttributeError:
            return       

    def select_folder(self):
        mas = tk.Toplevel()
        mas.withdraw()
        dir_path = filedialog.askdirectory()
        self.Box4.insert(0,str(dir_path))
        self.dir_path = dir_path          

    def go(self):
        Thread(target = self.spawn).start()

    def spawn(self):
                while True:
                    if self.processes['1'] == 0:
                        proc = 1
                        p = multiprocessing.Process(target=self.run(proc))
                        p.start()
                        p.join()                        
                        break
                    if self.processes['2'] == 0:
                        proc = 2
                        p2 = multiprocessing.Process(target=self.run(proc))
                        p2.start()
                        p2.join()  
                        break
                    if self.processes['3'] == 0:
                        proc = 3
                        p3 = multiprocessing.Process(target=self.run(proc))
                        p3.start()
                        p3.join() 
                        break
                    if self.processes['4'] == 0:
                        proc = 4
                        p4 = multiprocessing.Process(target=self.run(proc))
                        p4.start()
                        p4.join() 
                        break
                    if self.processes['5'] == 0:
                        proc = 5
                        p5 = multiprocessing.Process(target=self.run(proc))
                        p5.start()
                        p5.join() 
                        break
                    if self.processes['6'] == 0:
                        proc = 6 
                        p6 = multiprocessing.Process(target=self.run(proc))
                        p6.start()
                        p6.join() 
                        break 


    def run(self, process):
            while True:
                if self.processes['1'] == 0:
                    self.processes['1'] = process
                    self.text.delete('1.0', END)
                    me = 1
                    break
                if self.processes['2'] == 0:
                    self.processes['2'] = process
                    self.text2.delete('1.0', END)
                    me = 2
                    break
                if self.processes['3'] == 0:
                    self.processes['3'] = process
                    self.text3.delete('1.0', END)
                    me = 3
                    break
                if self.processes['4'] == 0:
                    self.processes['4'] = process
                    self.text4.delete('1.0', END)
                    me = 4
                    break
                if self.processes['5'] == 0:
                    self.processes['5'] = process
                    self.text5.delete('1.0', END)
                    me = 5
                    break
                if self.processes['6'] == 0:
                    self.processes['6'] = process
                    self.text6.delete('1.0', END)
                    me = 6
                    break
                else:
                    return
            time.sleep(2)
            self.text1 = "done 1/10"
            self.update(me)
            time.sleep(5)  
            self.text1 = "done 2/10"
            self.update(me) 
            time.sleep(3)             
            self.text1 = "done 3/10"
            self.update(me)
            time.sleep(8)
            self.text1 = "done 4/10"
            self.update(me)
            time.sleep(2) 
            self.text1 = "done 5/10"
            self.update(me)            
            if me== 1:
                self.processes['1'] = 0
            if me== 2:
                self.processes['2'] = 0
            if me== 3:
                self.processes['3'] = 0            
            if me== 4:
                self.processes['4'] = 0
            if me== 5:
                self.processes['5'] = 0
            if me== 6:
                self.processes['6'] = 0  

if __name__ == '__main__':     
    master = tk.Tk()
    ma = main(master)
    master.mainloop()

我是多处理技术的新手,所以如果您对如何正确使用它有很好的建议,我将非常感激。

0 个答案:

没有答案