Skip to content Skip to sidebar Skip to footer

Qrunnable In Multiple Cores

I am learning about QRunnable and I have the following code: from PyQt5.QtCore import QThreadPool, QRunnable class SomeObjectToDoComplicatedStuff(QRunnable): def __init__(self

Solution 1:

Not sure how much use this will be, but a multiprocessing version of your example script would be something like this:

from multiprocessing import Pool

classWorker(object):
    def__init__(self, name):
        self.name = name

    defrun(self):
        print('running', self.name)
        a = 10
        b = 30
        c = 0for i inrange(5000000):
            c += a**b
        print('done', self.name)
        return self.name, c

defcaller(worker):
    return worker.run()

defrun():
    pool = Pool()
    batch_size = 10
    workers = (Worker('object%d' % i) for i inrange(batch_size))
    result = pool.map(caller, workers)
    for item in result:
        print('%s = %s' % item)

if __name__ == '__main__':

    run()

Solution 2:

How can I make this code run using all the processor cores?

Using PyQt (QRunner/QThread and likely), I think it's almost impossible because they (the python version, not the C++) are using the GIL.

The easiest solution would be to use multiprocessing, but since you have some problem using it along scipy you should look for some non-standard library.

I suggest you to take a look at ipyparallel, AFAIK they're developed under the same umbrella, so they're likely to work seamlessy.

Post a Comment for "Qrunnable In Multiple Cores"