鱼C论坛

 找回密码
 立即注册
查看: 2475|回复: 5

[已解决]为什么在类里运行多进程就报错?

[复制链接]
发表于 2021-3-26 11:37:56 | 显示全部楼层 |阅读模式
20鱼币
为什么在类里运行多进程就报错?
点’执行‘按键报错,点’正常‘就没有报错,请问多进程应该怎么修改才可以在类中正常运行,谢谢!!!
  1. import multiprocessing as mp
  2. import tkinter as tk


  3. class Test:
  4.    
  5.     def __init__(self):
  6.         def job(q):
  7.             res = 0
  8.             for i in range(1000000):
  9.                 res += i + i**2 + i**3
  10.             q.put(res) # queue

  11.         def doit():
  12.             q1 = mp.Queue()
  13.             q2 = mp.Queue()
  14.             p1 = mp.Process(target=job, args=(q1,))
  15.             p2 = mp.Process(target=job, args=(q2,))
  16.             p1.start()
  17.             p2.start()
  18.             p1.join()
  19.             p2.join()
  20.             res1 = q1.get()
  21.             res2 = q2.get()
  22.             print('multicore:',res1 + res2)

  23.         def nomal():
  24.             res = 0
  25.             for a in range(2):
  26.                 for i in range(1000000):
  27.                     res += i + i**2 + i**3
  28.             print('nomal:',res)
  29.             

  30.         
  31.         root = tk.Tk()
  32.         frame = tk.Frame(root,height=400,width=400)
  33.         frame.grid(row=0)
  34.         theButton = tk.Button(frame,height=4,width=10,text='正常',command=nomal)
  35.         theButton.grid(row=0,column=0)
  36.         theButton1 = tk.Button(frame,height=4,width=10,text='执行',command=doit)
  37.         theButton1.grid(row=1,column=0)
  38.         root.mainloop()
  39.         

  40. if __name__ == '__main__':
  41.     go = Test()
复制代码
最佳答案
2021-3-26 11:37:57
  1. import multiprocessing as mp
  2. import tkinter as tk


  3. class Test():

  4.     def newtk(self):
  5.         root = tk.Tk()
  6.         frame = tk.Frame(root,height=400,width=400)
  7.         frame.grid(row=0)
  8.         theButton1 = tk.Button(frame,height=4,width=10,text='执行',command=self.doit)
  9.         theButton1.grid(row=1,column=0)
  10.         root.mainloop()
  11.      

  12.     def job(self,q):
  13.         res = 0
  14.         for i in range(1000000):
  15.             res += i + i**2 + i**3
  16.         q.put(res) # queue

  17.     def doit(self):
  18.         q = mp.Queue()
  19.         p1 = mp.Process(target=self.job, args=(q,))
  20.         p2 = mp.Process(target=self.job, args=(q,))
  21.         p1.start()
  22.         p2.start()
  23.         p1.join()
  24.         p2.join()
  25.         res1 = q.get()
  26.         res2 = q.get()
  27.         print('multicore:',res1 + res2)


  28.             

  29.         

  30.         

  31. if __name__ == '__main__':
  32.     go = Test()
  33.     go.newtk()
复制代码

最佳答案

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-3-26 11:37:57 | 显示全部楼层    本楼为最佳答案   
  1. import multiprocessing as mp
  2. import tkinter as tk


  3. class Test():

  4.     def newtk(self):
  5.         root = tk.Tk()
  6.         frame = tk.Frame(root,height=400,width=400)
  7.         frame.grid(row=0)
  8.         theButton1 = tk.Button(frame,height=4,width=10,text='执行',command=self.doit)
  9.         theButton1.grid(row=1,column=0)
  10.         root.mainloop()
  11.      

  12.     def job(self,q):
  13.         res = 0
  14.         for i in range(1000000):
  15.             res += i + i**2 + i**3
  16.         q.put(res) # queue

  17.     def doit(self):
  18.         q = mp.Queue()
  19.         p1 = mp.Process(target=self.job, args=(q,))
  20.         p2 = mp.Process(target=self.job, args=(q,))
  21.         p1.start()
  22.         p2.start()
  23.         p1.join()
  24.         p2.join()
  25.         res1 = q.get()
  26.         res2 = q.get()
  27.         print('multicore:',res1 + res2)


  28.             

  29.         

  30.         

  31. if __name__ == '__main__':
  32.     go = Test()
  33.     go.newtk()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-3-26 19:09:35 | 显示全部楼层
代码问题不大 缩进有问题 __init__ 函数和其他函数应该是并列关系。
【Spyder python3.6 测试通过】

  1. import multiprocessing as mp
  2. import tkinter as tk


  3. class Test():

  4.     def __init__(self):
  5.         root = tk.Tk()
  6.         frame = tk.Frame(root,height=400,width=400)
  7.         frame.grid(row=0)
  8.         theButton1 = tk.Button(frame,height=4,width=10,text='执行',command=self.doit)
  9.         theButton1.grid(row=1,column=0)
  10.         root.mainloop()
  11.      

  12.     def job(self,q):
  13.         res = 0
  14.         for i in range(1000000):
  15.             res += i + i**2 + i**3
  16.         q.put(res) # queue

  17.     def doit(self):
  18.         q = mp.Queue()
  19.         p1 = mp.Process(target=self.job, args=(q,))
  20.         p2 = mp.Process(target=self.job, args=(q,))
  21.         p1.start()
  22.         p2.start()
  23.         p1.join()
  24.         p2.join()
  25.         res1 = q.get()
  26.         res2 = q.get()
  27.         print('multicore:',res1 + res2)


  28.             

  29.         

  30.       
  31. if __name__ == '__main__':
  32.     go = Test()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-3-26 19:18:38 | 显示全部楼层
楼主我最近也在学多线程 一起交流啊 想结合UI 用多线程防止界面未响应 或者卡顿
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-3-26 21:36:34 | 显示全部楼层
591821661 发表于 2021-3-26 19:18
楼主我最近也在学多线程 一起交流啊 想结合UI 用多线程防止界面未响应 或者卡顿

我是用到才学的,完成代码就不再学了,
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-3-27 10:14:53 | 显示全部楼层

如果方便的话,麻烦帮我看一下另一个问题,谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-26 03:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表