简体   繁体   English

在 python 中的多处理之前限制 cpu 内核

[英]Limiting cpu cores before multiprocessing in python

I have a program which requires multiprocessing.我有一个需要多处理的程序。 The function that it calls will automatically use every available core.它调用的 function 将自动使用每个可用的内核。 This somehow causes a problem however, as every core is used for each of the processes, meaning each core has 100*x % load where x is the number of processes spawned.然而,这会导致一个问题,因为每个核心都用于每个进程,这意味着每个核心都有 100*x % 的负载,其中 x 是生成的进程数。 So for 6 processes, each sore is at 600% use.因此,对于 6 个过程,每个疮的使用率为 600%。

The code is quite simple and uses the usual:该代码非常简单,并使用通常的:

pool = Pool(processes=6)
for i in pool.imap_unordered(main_program, range(100)):
    print('Task in pool has finished')

This will however put every core at 600% load and be slower than doing every process individually.然而,这将使每个核心处于 600% 的负载,并且比单独执行每个进程要慢。 I assume I am using the mp module wrong, but I can't seem to figure out where.我假设我错误地使用了 mp 模块,但我似乎无法弄清楚在哪里。

Note: My ideal solution would be limiting the main function to be only using 1 core, however the function is not mine, but rather an application I call, and I would not know where to limit this in the source code.注意:我理想的解决方案是将主 function 限制为仅使用 1 个内核,但是 function 不是我的,而是我调用的应用程序,我不知道在源代码中在哪里限制它。

Any suggestions?有什么建议么?

Many thanks非常感谢

I found the answer here我在这里找到了答案

Basically, the BLAS features (of I suspect numpy) were interfering with my multiprocessing.基本上,BLAS 功能(我怀疑是 numpy 的)干扰了我的多处理。 This fixed it:这修复了它:

os.environ["OMP_NUM_THREADS"] = "1" # export OMP_NUM_THREADS=1
os.environ["OPENBLAS_NUM_THREADS"] = "1" # export OPENBLAS_NUM_THREADS=1
os.environ["MKL_NUM_THREADS"] = "1" # export MKL_NUM_THREADS=1
os.environ["VECLIB_MAXIMUM_THREADS"] = "1" # export VECLIB_MAXIMUM_THREADS=1
os.environ["NUMEXPR_NUM_THREADS"] = "1" # export NUMEXPR_NUM_THREADS=1

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

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