python - 在 python 中使用多处理时无法使用所有处理器

标签 python python-3.x python-multiprocessing

我正在研究 python 并尝试学习多处理。当我尝试以下代码时,它应该并行运行,但它只使用一个处理器。我无法理解原因。可能是什么问题,为什么它没有使用我电脑的所有 4 个内核。

我试过的代码如下:

import multiprocessing
import time
start = time.perf_counter()

def do_something():
    print("hello")
    time.sleep(1)
    print("done")

p1 = multiprocessing.Process(target=do_something())
p2 = multiprocessing.Process(target=do_something())
p1.start()
p2.start()
p1.join()
p2.join()
finish = time.perf_counter()
print(f'finished in {round(finish-start,1)} sec')

结果:

hello
done
hello
done
finished in 2.1 sec

它应该在 1 秒(大约)内执行

#

我用来查找核心数的代码:

#
import multiprocessing
print("Number of cpu : ", multiprocessing.cpu_count())

结果:

Number of cpu :  4

#

我尝试的另一个代码是:

#
from multiprocessing import Lock, Process, Queue, current_process
import time
import queue # imported for using queue.Empty exception


def do_job(tasks_to_accomplish, tasks_that_are_done):
    while True:
        try:
            '''
                try to get task from the queue. get_nowait() function will 
                raise queue.Empty exception if the queue is empty. 
                queue(False) function would do the same task also.
            '''
            task = tasks_to_accomplish.get_nowait()
        except queue.Empty:

            break
        else:
            '''
                if no exception has been raised, add the task completion 
                message to task_that_are_done queue
            '''
            print(task)
            tasks_that_are_done.put(task + ' is done by ' + current_process().name)
            time.sleep(.5)
    return True


def main():
    number_of_task = 10
    number_of_processes = 4
    tasks_to_accomplish = Queue()
    tasks_that_are_done = Queue()
    processes = []

    for i in range(number_of_task):
        tasks_to_accomplish.put("Task no " + str(i))

    # creating processes
    for w in range(number_of_processes):
        p = Process(target=do_job(tasks_to_accomplish, tasks_that_are_done))
        processes.append(p)
        p.start()

    # completing process
    for p in processes:
        p.join()

    # print the output
    while not tasks_that_are_done.empty():
        print(tasks_that_are_done.get())

    return True


if __name__ == '__main__':
    main()

结果:

Task no 0
Task no 1
Task no 2
Task no 3
Task no 4
Task no 5
Task no 6
Task no 7
Task no 8
Task no 9
Task no 0 is done by MainProcess
Task no 1 is done by MainProcess
Task no 2 is done by MainProcess
Task no 3 is done by MainProcess
Task no 4 is done by MainProcess
Task no 5 is done by MainProcess
Task no 6 is done by MainProcess
Task no 7 is done by MainProcess
Task no 8 is done by MainProcess
Task no 9 is done by MainProcess

#

根据建议,我做了以下几项更改:

import multiprocessing
import time
start = time.perf_counter()

def do_something():
    print("hello")
    time.sleep(1)
    print("done")

p1 = multiprocessing.Process(target=do_something)
p2 = multiprocessing.Process(target=do_something)
p1.start()
p2.start()
p1.join()
p2.join()
finish = time.perf_counter()
print(f'finished in {round(finish-start,1)} sec')

我得到的结果是:

finished in 0.2 sec

最佳答案

目标必须是可调用的。

p1 = multiprocessing.Process(target=do_something)

按照您的方式,您在主进程中调用方法并将结果传递给多处理。

关于python - 在 python 中使用多处理时无法使用所有处理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58590954/

相关文章:

python - 如何根据 Pandas 数据框的条件增加计数器?

python - numpy 结构化数组中的子数组不连续

python - 将对角矩阵提高到负幂 1/2

python - Django - 基于类的 View 返回空白页

Python - 我的频率函数效率低下

json - Python 3 json.load() 以错误的顺序读取 JSON 文件

python - 使用 python 进行 One Hot 编码的快速方法

python - 如何使用多处理模块终止进程?

python - 多处理python不并行运行

python - 多处理过程完成后如何退出python脚本?