I am learning about QRunnable and I have the following code:
from PyQt5.QtCore import QThreadPool, QRunnable
class SomeObjectToDoComplicatedStuff(QRunnable):
def __init__(self, name):
QRunnable.__init__(self)
self.name = name
def run(self):
print('running', self.name)
a = 10
b = 30
c = 0
for i in range(5000000):
c += a**b
print('done', self.name)
pool = QThreadPool.globalInstance()
pool.setMaxThreadCount(10)
batch_size = 100
workers = [None] * batch_size
for i in range(batch_size):
worker = SomeObjectToDoComplicatedStuff('object ' + str(i))
workers[i] = worker
pool.start(worker)
print('All cued')
pool.waitForDone()
# processing the results back
for i in range(batch_size):
print(workers[i].name, ' - examining again.')
I see that indeed there are different processes being alternated, but all is happening on a single core.
How can I make this code run using all the processor cores?
PS: This code is just a simplification of a super complicated number crunching application I am making. In it, I want to to do Monte Carlo in several threads and the worker itself is a complex optimization problem. I have tried the python multiprocessing module but it doesn't handle scipy too well.