简体   繁体   English

多处理:我想更好地理解这段代码

[英]Multiprocessing: I would like to understand this code better

I have found the following lines of code to compute the array mx by the repeated calling of a function called fun .我发现以下代码行通过重复调用名为fun的函数来计算数组mx

However, I would like to understand better what it does.但是,我想更好地了解它的作用。

Also, I assigned 16 cores to the parallel pool, however, I noticed that during computations no more than 2 cores are running at the same time.此外,我为并行池分配了 16 个内核,但是,我注意到在计算过程中同时运行的内核不超过 2 个。

Could someone explain what this code does and why it could be that only part of the threads is working?有人可以解释这段代码的作用以及为什么只有部分线程在工作吗?

Thank you!谢谢!

from tqdm import tqdm
from multiprocessing import Pool
from functools import partial

with Pool(processes = 16) as p_mx: 
    mx = tqdm(p_mx.imap(partial(fun, L), nodes), total = n)

multiprocessing.Pool() slower than just using ordinary functions multiprocessing.Pool() 比仅使用普通函数慢

The function you are trying to parallelize doesn't require enough CPU resources (ie CPU time) to rationalize parallelization!您尝试并行化的函数不需要足够的 CPU 资源(即 CPU 时间)来合理化并行化!

And may caused by the way Python handle multi-threading and multi-processing with the GIL:并且可能是由 Python 使用 GIL 处理多线程和多处理的方式引起的:

When to use threading and how many threads to use 何时使用线程以及使用多少线程

Look at the GIL , you will have a better understanding of why.看看GIL ,你会更好地理解为什么。

If you want concurrent code in Python 3.8, you have CPU-bound concurrency problems then this could be the ticket!如果你想要 Python 3.8 中的并发代码,你有 CPU 绑定的并发问题,那么这可能是票!

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

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