|
20鱼币
运行多进程就报错,是tkinter变量导致的,请问要怎么修改,谢谢!!
import multiprocessing as mp
import tkinter as tk
class Test():
def __init__(self):
root = tk.Tk()
frame = tk.Frame(root,height=400,width=400)
frame.grid(row=0)
self.v = tk.StringVar()
entry = tk.Entry(frame, textvariable=self.v,width=3)
entry.grid(row=0, column=0)
entry.insert(0,10)
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):
#print(self.v.get())
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()
import multiprocessing as mp
import tkinter as tk
class Test():
def __init__(self):
global v
root = tk.Tk()
frame = tk.Frame(root,height=400,width=400)
frame.grid(row=0)
v = tk.StringVar()
entry = tk.Entry(frame, textvariable=v,width=3)
entry.grid(row=0, column=0)
entry.insert(0,10)
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):
print(v.get())
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()
|
|