Search code examples
pythonpython-multiprocessing

Python multiprocesing pool opens new window


Good evening all,

I have created a CPU heavy function which I need to iterate 50 times with input from a list and then I am using the multiprocessing pool to synchronously compute the 50 functions. It works but creates a new window for each new process and only after I close these windows I do get the result. Is there a way to not open the window each time? I have tried multiple multiprocessing methods, but all do the same.

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()

I don't know if the problem is in bigF(list) or the multiprocessing way. I aim to shorten the processing time to less than 2 seconds where 1 bigF(list) takes around 0.5 seconds.

Thank you for any suggestions and comments.


Solution

  • You need to read the cautions in the multiprocessing doc. When your secondary processes start, it starts a new copy of the interpreter and re-executes your main code. That includes starting a new main window. Anything that should be run in the main process only needs to be inside if __name__=='__main__':. So you need:

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