|
20鱼币
为什么在类里运行多进程就报错?
点’执行‘按键报错,点’正常‘就没有报错,请问多进程应该怎么修改才可以在类中正常运行,谢谢!!!
- import multiprocessing as mp
- import tkinter as tk
- class Test:
-
- def __init__(self):
- def job(q):
- res = 0
- for i in range(1000000):
- res += i + i**2 + i**3
- q.put(res) # queue
- def doit():
- q1 = mp.Queue()
- q2 = mp.Queue()
- p1 = mp.Process(target=job, args=(q1,))
- p2 = mp.Process(target=job, args=(q2,))
- p1.start()
- p2.start()
- p1.join()
- p2.join()
- res1 = q1.get()
- res2 = q2.get()
- print('multicore:',res1 + res2)
- def nomal():
- res = 0
- for a in range(2):
- for i in range(1000000):
- res += i + i**2 + i**3
- print('nomal:',res)
-
-
- root = tk.Tk()
- frame = tk.Frame(root,height=400,width=400)
- frame.grid(row=0)
- theButton = tk.Button(frame,height=4,width=10,text='正常',command=nomal)
- theButton.grid(row=0,column=0)
- theButton1 = tk.Button(frame,height=4,width=10,text='执行',command=doit)
- theButton1.grid(row=1,column=0)
- root.mainloop()
-
- if __name__ == '__main__':
- go = Test()
复制代码
- import multiprocessing as mp
- import tkinter as tk
- class Test():
- def newtk(self):
- root = tk.Tk()
- frame = tk.Frame(root,height=400,width=400)
- frame.grid(row=0)
- theButton1 = tk.Button(frame,height=4,width=10,text='执行',command=self.doit)
- theButton1.grid(row=1,column=0)
- root.mainloop()
-
- def job(self,q):
- res = 0
- for i in range(1000000):
- res += i + i**2 + i**3
- q.put(res) # queue
- def doit(self):
- q = mp.Queue()
- p1 = mp.Process(target=self.job, args=(q,))
- p2 = mp.Process(target=self.job, args=(q,))
- p1.start()
- p2.start()
- p1.join()
- p2.join()
- res1 = q.get()
- res2 = q.get()
- print('multicore:',res1 + res2)
-
-
-
- if __name__ == '__main__':
- go = Test()
- go.newtk()
复制代码
|
|