IT Ninja
IT Ninja

Reputation: 6430

Tkinter GUI freeze while multiprocessing/subprocessing

So i am running into a major issue. I am currently trying to use multi-processing/sub-processing to run files next to my Tkinter application, however, as soon as I run the process, the GUI freezes until I finish the process. Is there any way to get around this? I have also looked at other questions on SO but to no avail (I found one suggesting root.update() however that is not working as expected.

Note: I have not included GUI elements, as I have made basic programs to try this (only a couple of lines) and get the same problem. It also may be worth noting that I am running windows.

code (taken out of context):

def run_file(self):
    self.root.update()
    sub_process=subprocess.call(self.sub_proc_args)

process=multiprocessing.Process(target=self.run_file())
process.start()

Upvotes: 2

Views: 1769

Answers (2)

IT Ninja
IT Ninja

Reputation: 6430

I have found the problem (other then the target issue that BrenBarn pointed out). Here is my fix:

(i took out the multiprocessing.)

def run_file(self):
    sub_process=subprocess.Popen(self.sub_proc_args) #Open subprocess
    self.root.update() #Update GUI    
self.run_file() #Initiate sub_process

I have found that this is the fix because using call, when you execute it, it must return a return value, which makes Tkinter not continue its mainloop. This is fixed by using Popen.

Upvotes: 1

BrenBarn
BrenBarn

Reputation: 251345

By doing self.run_file() you are calling run_file before multiprocessing can use it. You need to use target=self.run_file (note, without parentheses).

Upvotes: 1

Related Questions