简体   繁体   English

Python 多处理始终使用 x 核数

[英]Python multiprocessing to always utilize x number of cores

I have the following code that uses python's multiprocessing library to do some heavy computation using 8 cores.我有以下代码使用 python 的多处理库使用 8 个内核进行一些繁重的计算。

import random
from multiprocessing import Process
random_numbers_list = [random.random()] * 10000000 

for i in range(0, len(random_numbers_list), 8):
    threads = []
    for j in range(i, i + 8):
        if j > len(random_numbers_list) - 1:
            break
        p = Process(target=do_heavy_computation,
                    args=(random_numbers_list[j], j))
        threads.append(p)

    [t.start() for t in threads]
    [t.join() for t in threads]

However, the problem is, while all 8 cores is correctly utilized to do the computation in parallel, it waits for the current 8 cores batch to finish until the next 8 cores batch starts.然而,问题是,虽然所有 8 核都被正确地用于并行计算,但它会等待当前 8 核批处理完成,直到下一个 8 核批处理开始。 I would like the code to always utilize 8 cores, so that when any of the cores are done, it uses the next available core out of the total 8 cores.我希望代码始终使用 8 个内核,这样当任何一个内核完成时,它会使用总共 8 个内核中的下一个可用内核。 Any guidance would be greatly appreciated!任何指导将不胜感激!

Let a process pool take care of everything for you.让进程池为您处理一切。

from multiprocessing import Pool

with Pool(processes=8) as pool:
    for index, value in enumerate(random_numbers_list):
        pool.apply(do_heavy_computation, args=(value, index))

The Pool() initializer has additional arguments that you can investigate. Pool()初始化程序有额外的 arguments 可以调查。 But the above code is all you need to run your code on 8 processes, and wait until it's finished.但是上面的代码是你在 8 个进程上运行代码所需的全部,并等待它完成。

The most important argument you may need to change is maxtasksperchild , whose default value is None .您可能需要更改的最重要的参数是maxtasksperchild ,其默认值为None Eight threads will be created and all your tasks will be run on those eight threads.将创建八个线程,您的所有任务都将在这八个线程上运行。 You can change this to masktasksperchild=1 , and each thread will run only 1 task, and then die and be replaced by a fresh thread.您可以将其更改为masktasksperchild=1 ,每个线程将只运行 1 个任务,然后死亡并被新线程替换。 That's what you're doing above, but it's unclear if that's necessary.这就是您在上面所做的,但尚不清楚这是否有必要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM