Python 多处理只使用一个内核

标签 python ubuntu multiprocessing

我正在尝试来自 standard python documentation 的代码片段学习如何使用多处理模块。代码粘贴在此消息的末尾。 我在四核机器上的 Ubuntu 11.04 上使用 Python 2.7.1(根据系统监视器,由于超线程,它给了我八个内核)

问题:所有工作负载似乎都被安排到一个核心上,尽管有多个进程已启动,但该核心的利用率接近 100%。有时所有工作负载都会迁移到另一个核心,但工作负载永远不会在它们之间分配。

知道为什么会这样吗?

最好的问候,

保罗

#
# Simple example which uses a pool of workers to carry out some tasks.
#
# Notice that the results will probably not come out of the output
# queue in the same in the same order as the corresponding tasks were
# put on the input queue.  If it is important to get the results back
# in the original order then consider using `Pool.map()` or
# `Pool.imap()` (which will save on the amount of code needed anyway).
#
# Copyright (c) 2006-2008, R Oudkerk
# All rights reserved.
#

import time
import random

from multiprocessing import Process, Queue, current_process, freeze_support

#
# Function run by worker processes
#

def worker(input, output):
    for func, args in iter(input.get, 'STOP'):
        result = calculate(func, args)
        output.put(result)

#
# Function used to calculate result
#

def calculate(func, args):
    result = func(*args)
    return '%s says that %s%s = %s' % \
        (current_process().name, func.__name__, args, result)

#
# Functions referenced by tasks
#

def mul(a, b):
    time.sleep(0.5*random.random())
    return a * b

def plus(a, b):
    time.sleep(0.5*random.random())
    return a + b


def test():
    NUMBER_OF_PROCESSES = 4
    TASKS1 = [(mul, (i, 7)) for i in range(500)]
    TASKS2 = [(plus, (i, 8)) for i in range(250)]

    # Create queues
    task_queue = Queue()
    done_queue = Queue()

    # Submit tasks
    for task in TASKS1:
        task_queue.put(task)

    # Start worker processes
    for i in range(NUMBER_OF_PROCESSES):
        Process(target=worker, args=(task_queue, done_queue)).start()

    # Get and print results
    print 'Unordered results:'
    for i in range(len(TASKS1)):
       print '\t', done_queue.get()

    # Add more tasks using `put()`
    for task in TASKS2:
        task_queue.put(task)

    # Get and print some more results
    for i in range(len(TASKS2)):
        print '\t', done_queue.get()

    # Tell child processes to stop
    for i in range(NUMBER_OF_PROCESSES):
        task_queue.put('STOP')

test()

最佳答案

尝试用实际需要 CPU 的东西替换 time.sleep,您会看到 multiprocess 工作得很好!例如:

def mul(a, b):
    for i in xrange(100000):
        j = i**2
    return a * b

def plus(a, b):
    for i in xrange(100000):
        j = i**2
    return a + b

关于Python 多处理只使用一个内核,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6905264/

相关文章:

Python LDAP 将属性写入 Active Directory

python - 一次为 pandas 数据框中的 matplotlib 子图添加多个标签

linux - 在 Linux 中使用 Chrome 通过 Selenium 运行 NaCl 辅助进程,不会出现沙箱错误

c++ - 添加断点并运行程序时无法访问地址处的内存

ubuntu - 假脱机的后缀重新处理 - ubuntu

python - 多处理时保留类实例列表内容

Python动态、多线程变量不起作用

python - 如何让pycharm远程执行使用系统环境变量?

python - 使用 dataframe.loc 时无法将 dtyped [datetime64[ns]] 数组与 [bool] 类型的标量进行比较的错误

python - 在 Python 中有效地递归文件目录,同时最大限度地减少内存使用