QRunnable 多核 [英] QRunnable in multiple cores

查看:48
本文介绍了QRunnable 多核的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 QRunnable 并且我有以下代码:

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.

如何使此代码使用所有处理器内核运行?

PS:这段代码只是我正在制作的一个超级复杂的数字运算应用程序的简化.在其中,我想在多个线程中进行蒙特卡罗,而 worker 本身就是一个复杂的优化问题.我尝试过 python 多处理模块,但它不能很好地处理 scipy.

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.

推荐答案

不确定这会有多大用处,但是一个 多处理 版本的示例脚本将是这样的:

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

class Worker(object):
    def __init__(self, name):
        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)
        return self.name, c

def caller(worker):
    return worker.run()

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

if __name__ == '__main__':

    run()

这篇关于QRunnable 多核的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆