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?
exitcodeof those processes? I just trying on my machine and they exit with1, i.e. they got an error. I believe this is due to the fact that you cannot pass methods astargetarguments. Thetargetmust be picklable, hence it should be a function defined at the top level of a module(see documentation forpickleandmultiprocessingto understand why). - Bakuriumainloopin your GUI process? - Bryan Oakley