0
votes

I have a GUI class that merely has instances of tkinter widgets and is not a subclass of tkinter. This GUI class also has an instance of a data processing object, of which it calls a member function that takes a long time to execute. In an attempt to prevent the GUI from freezing and to update a progress bar, I use multiprocessing.

outqueue = mp.Queue()
objqueue = mp.Queue()
progqueue = mp.Queue()
try:
     process = mp.Process(target=self.vromad.extractPlayers_mp,args=[outqueue, objqueue, progqueue])
     process2 = mp.Process(target=self.vromad.extractPlayers)
     process2.daemon = True
     process.daemon = True
     process.start()
     process2.start()
     print("started process")
     self.frame.after(500, self.updateBar, progqueue)
     print("bar should have started")
except:
     self.exceptionPopUp(traceback.format_exc())
     xtractStatus = -1
print("already here") 

Here is where the behavior really gets interesting. I run a second process which is identical to the first one to check if there's any blocking going on by the multiprocessing. Nope, "started process" prints before either task finishes. In fact, CPU usage exactly doubles by adding process2.start(). Additionally, "bar should have started" and "already here" are printed BEFORE the bar actually updates. In fact, the callback self.updateBar prints output AFTER "already here."

It really looks like while the code continues to exceute, all GUI events are deferred until everything is idle. This really looks to be the case, as not only does the bar not update until the processes have finished, but the entire window freezes. I have tried delay times for self.frame.after as short as 10ms and as long as 1000ms, with no change in behavior. Is this because the process somehow gets some attribute of tkinter that blocks tkinter from going through its event loop? Would switching to threading help here?

1
Did you check the exitcode of those processes? I just trying on my machine and they exit with 1, i.e. they got an error. I believe this is due to the fact that you cannot pass methods as target arguments. The target must be picklable, hence it should be a function defined at the top level of a module(see documentation for pickle and multiprocessing to understand why). - Bakuriu
I'm getting an exitcode of 0, even when I pass the member function of a member object as the target. - Loser777
Are you running mainloop in your GUI process? - Bryan Oakley
Yes, the GUI class has a member function that just calls self.root.mainloop(). This is started in the main process that runs everything except for the function that takes a long time. - Loser777

1 Answers

0
votes

I found out what was wrong. While the command that was handling the button press was spawning a new process to do the work, it was blocking with somequeue.get() and waiting for the work to finish. Since the command handling runs in the same thread as tkinter's mainloop(), mainloop() events were being blocked, causing the problem seen here. This is solved by moving the somequeue.get() code to another function that doesn't block until all of the work is done. This function also updates the progress bar, while checking the queue for some sentinel value showing that all the work is done.

That's the emphasis. That function blocks ONLY when all the work is done.