简体   繁体   English

将所有内核与 python 多处理一起使用

[英]Use all cores with python multiprocessing

I wrote a test-purpose snippet to use multiprocessing to work on all cores of my laptop.我编写了一个用于测试的片段,以使用多处理在我的笔记本电脑的所有内核上工作。 I have a 8 core cpu.我有一个8核CPU。 Below the (basic) code:在(基本)代码下方:

import os
import time
import multiprocessing


def worker(n):
    pid = os.getpid()
    for x in range(0, 10):
        print("PID: %s   INPUT: %s" % (str(pid), str(n)))
        time.sleep(2)


input_params_list = [1, 2, 3, 4, 5, 6, 7, 8]
pool = multiprocessing.Pool(8)
pool.map(worker, input_params_list)
pool.close()
pool.join()

Basically it should start 8 processes which should just print their pid and the integer they get as input parameter.基本上它应该启动 8 个进程,这些进程应该只打印它们的 pid 和它们作为输入参数获得的 integer。 I just added a sleep to introduce some delay and make all of them running in parallel.我只是添加了一个 sleep 来引入一些延迟并让所有这些都并行运行。 When I run the script this is what I get:当我运行脚本时,这就是我得到的:

PID: 811   INPUT: 1
PID: 812   INPUT: 2
PID: 813   INPUT: 3
PID: 814   INPUT: 4
PID: 815   INPUT: 5
PID: 816   INPUT: 6
PID: 817   INPUT: 7
PID: 818   INPUT: 8
PID: 811   INPUT: 1
PID: 812   INPUT: 2
PID: 813   INPUT: 3
PID: 814   INPUT: 4
PID: 815   INPUT: 5
PID: 816   INPUT: 6
PID: 817   INPUT: 7
PID: 818   INPUT: 8
... ... ... ... ...
... ... ... ... ...

I see that I have 8 different processes (plus the "father") running at the same time.我看到我有 8 个不同的进程(加上“父亲”)同时运行。 The problem is that I think they're not running on 8 different cores.问题是我认为它们没有在 8 个不同的内核上运行。 This is what I get from htop (I get the same with top too):这是我从 htop 得到的(我也和 top 一样): 在此处输入图像描述

As I understood, the CPU column should contain the number of the core the process is running on.据我了解,CPU 列应包含进程正在运行的核心数。 In this case I think that something is not working as expected since it is 1 for all of them.在这种情况下,我认为某些东西没有按预期工作,因为它们都是 1。 Otherwise I suppose there's something I misunderstood or something wrong in my code.否则我想我的代码中有什么我误解或错误的地方。

MisterMiyagi is right.宫城先生是对的。 To show that CPUs are working, I changed your code a bit, and add a bit of CPU-bound task to calculate the factorial of a big number to last for a few seconds (CPUs are on fire).为了显示 CPU 正在工作,我稍微更改了您的代码,并添加了一些 CPU 绑定任务来计算一个大数的阶乘以持续几秒钟(CPU 着火了)。 Also, I use the private variable _identity to see what core the worker runs on.此外,我使用私有变量_identity来查看worker运行在哪个内核上。

import os
import time
import multiprocessing
import numpy as np

def worker(n):
    factorial = np.math.factorial(900000)
    # rank = multiprocessing.current_process().name one can also use
    rank = multiprocessing.current_process()._identity[0]
    print(f'I am processor {rank}, got n={n}, and finished calculating the factorial.')


cpu_count = multiprocessing.cpu_count()
input_params_list = range(1, cpu_count+1)


pool = multiprocessing.Pool(cpu_count)
pool.map(worker, input_params_list)
pool.close()
pool.join()

output output

I am processor 4, got n=4, and finished calculating the factorial.
I am processor 2, got n=2, and finished calculating the factorial.
I am processor 1, got n=1, and finished calculating the factorial.
I am processor 3, got n=3, and finished calculating the factorial.

在此处输入图像描述

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

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