Python 多处理池打开新 window

Python multiprocesing pool opens new window

大家晚上好,

我创建了一个 CPU 重函数,我需要使用列表中的输入迭代 50 次,然后我使用多处理池同步计算这 50 个函数。它可以工作,但会为每个新进程创建一个新的 window,只有在我关闭这些 windows 之后,我才会得到结果。有没有办法每次都不打开window?我尝试了多种多处理方法,但都是一样的。

import sqlite3
import random
import time
import concurrent.futures
from multiprocessing import Pool
from tkinter import *
import time

window = Tk()

window.title("APP")

def bigF(list):
            n=list[0]   # Extract multiple var from list
            n2=list[1]
            
            new = f(n)  # The CPU heavy computation.
            
            if n2 == new:
                return True
            else:
                return False

def Screen1():
           
    window.geometry("350x250")
    
    # Lables and entry windows.
        
    btn = Button(window, text='S2', command=Screen2)
    btn.pack(pady=10)
    
def Screen2():
            
    window.geometry("350x220")
    
    # Lables and entry windows.
    
    def getP():
        user = user.get()
        cursor.execute(database)
        try:
           return cursor.fetchall()[0][0]
        except IndexError:
           raise ValueError('')
    
    def getS():
        user = user.get()
        cursor.execute(database)
        return cursor.fetchall()[0][0]
    
    def getS2():
        user = user.get()
        cursor.execute(database)
        return cursor.fetchall()[0][0]
    
    def function():
        
        try:
            var = getP()
        except ValueError:
            return False
        s = getS()
        s2 = getS2()
        t = t.get()
                    
        if __name__ == '__main__':
            list1=[]
            listt=[]
            list3=[]
            list1[:0]=somestring
            random.shuffle(list1)
            
            for i in range(len(list1)):
                listt.append(list1[i])
                listt.append(var)
                listt.append(s)
                listt.append(s2)
                listt.append(t)
                list3.append(listt)
                listt=[]
            with Pool(50) as p:
                values = p.map(bigF, list3)
        
        if True in values:
                someF()
        else:
                # Lables

         
    btn = Button(window, text='function', command=function)
    btn.pack(pady=10)
    
    sgn =  Button(window, text='S1', command=Screen1)
    sgn.pack(padx=10, side=RIGHT)
    
def someF():
            
    window.geometry("350x350")
    # Lables and entry windows.

Screen1()
window.mainloop()

不知道是bigF(list)的问题还是multiprocessing方式的问题。我的目标是将处理时间缩短到不到 2 秒,其中 1 个 bigF(list) 大约需要 0.5 秒。

感谢您提出任何建议和意见。

您需要阅读 multiprocessing 文档中的注意事项。当您的辅助进程启动时,它会启动一个新的解释器副本并重新执行您的主代码。这包括开始一个新的主 window。任何应该在主进程中 运行 的东西只需要在 if __name__=='__main__': 里面。所以你需要:

if __name__ == '__main__':
    windows = Tk()
    window.title("APP")
    Screen1()
    window.mainloop()