3

我在网上找到了这段代码,预测结果应该显示当前正在处理的进程名称,但是当我运行代码时,它只给我结果而不是名称。我正在运行 Windows 7 Python 2.7.3。

代码:

import multiprocessing

def do_calculation(data):
    return data * 2

def start_process():
    print 'Starting', multiprocessing.current_process().name

if __name__ == '__main__':
    inputs = list(range(10))
    print 'Input   :', inputs

    builtin_outputs = map(do_calculation, inputs)
    print 'Built-in:', builtin_outputs

    pool_size = multiprocessing.cpu_count() * 2
    pool = multiprocessing.Pool(processes=pool_size,
                                initializer=start_process,
                                )
    pool_outputs = pool.map(do_calculation, inputs)
    pool.close() # no more tasks
    pool.join()  # wrap up current tasks

    print 'Pool    :', pool_outputs

预期结果:

$ python multiprocessing_pool.py

Input   : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Built-in: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Starting PoolWorker-11
Starting PoolWorker-12
Starting PoolWorker-13
Starting PoolWorker-14
Starting PoolWorker-15
Starting PoolWorker-16
Starting PoolWorker-1
Starting PoolWorker-2
Starting PoolWorker-3
Starting PoolWorker-4
Starting PoolWorker-5
Starting PoolWorker-8
Starting PoolWorker-9
Starting PoolWorker-6
Starting PoolWorker-10
Starting PoolWorker-7
Pool    : [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
4

1 回答 1

1

如果我在控制台而不是IDLE中运行以下代码(使用 PyCharm 后我继续返回):

from multiprocessing import *

def main():
    data = list(range(10))
    print('Data:', data)

    result = list(map(calculate, data))
    print('Map: ', result)

    pool = Pool(cpu_count() * 2, initialize)
    result = pool.map(calculate, data, 1)
    pool.close()
    pool.join()

    print('Pool:', result)

def calculate(number):
    return number * 2

def initialize():
    print('Starting', current_process().name)

if __name__ == '__main__':
    main()

如您所料,以下内容会打印到控制台屏幕(PoolWorkers 的顺序是随机的):

C:\Users\schappell\Desktop>stack_overflow.py
Data: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Map:  [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Starting PoolWorker-1
Starting PoolWorker-13
Starting PoolWorker-2
Starting PoolWorker-3
Starting PoolWorker-8
Starting PoolWorker-5
Starting PoolWorker-14
Starting PoolWorker-16
Starting PoolWorker-15
Starting PoolWorker-12
Starting PoolWorker-6
Starting PoolWorker-9
Starting PoolWorker-4
Starting PoolWorker-10
Starting PoolWorker-7
Starting PoolWorker-11
Pool: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

C:\Users\schappell\Desktop>

作为旁注,请尝试运行以下代码(有和没有time.sleep注释掉):

from multiprocessing import *
import time

def main():
    data = list(range(10))
    print('Data:', data)
    print('Map: ', list(map(calculate, data)))
    pool = Pool(cpu_count() * 2, initialize)
##    time.sleep(1)
    print('Pool:', pool.map(calculate, data, 1))
    pool.close()
    pool.join()

def calculate(number):
    return number * 2

def initialize():
    print('Starting', current_process().name)

if __name__ == '__main__':
    main()

没有睡眠,你可能会得到这样的东西(显示你的池还没有完全创建):

C:\Users\schappell\Desktop>stack_overflow.py
Data: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Map:  [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Starting PoolWorker-2
Pool: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Starting PoolWorker-1
Starting PoolWorker-10
Starting PoolWorker-9
Starting PoolWorker-6
Starting PoolWorker-14
Starting PoolWorker-13
Starting PoolWorker-5
Starting PoolWorker-4
Starting PoolWorker-3
Starting PoolWorker-8
Starting PoolWorker-11
Starting PoolWorker-12
Starting PoolWorker-16
Starting PoolWorker-7
Starting PoolWorker-15

C:\Users\schappell\Desktop>
于 2013-07-10T19:05:44.233 回答