Reputation: 5758
I have a GUI and, by the click of a button I have to start a big calculation.
I am using QtDesigner with python 2.7
At the beginning, I had the problem that my GUI became unresponsive while the big calculation was working.
I solved this using the Threading module:
class myMainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
#rest of the code here
class heavyCalculations(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
#rest of the code here
My doubt now is: Let's say that I want to start 4 heavy calculations, and I want to use all 4 cores of my computer working on time. For that I will need multiprocessing, but what should I do to avoid my GUI to become unresponsive, but at the same time to use 4 cores on time?
As far as I know, If I start a new process for each heavy calculation it works in parallel indeed, but my GUI becomes unresponsive.
And if I start 4 threads, it also works and the GUI is not affected, but this is not real parallel.
Any help?
Upvotes: 0
Views: 327
Reputation: 73294
If you don't want your calculation processes to slow down your GUI process, you should make sure your GUI process runs at a higher priority than your calculation process. That way, whenever there is contention for a core (i.e. when there are more processes simultaneously wanting to run than your computer has CPU cores to run them on), the GUI process will get first dibs and the calculation process will have to wait until the GUI has finished doing whatever it wanted to do.
The easiest way to achieve that is to have your calculation processes lower their own priority as the first thing they do, before they start their calculations. As for how to get a Python process to lower its priority, see here.
Upvotes: 1