python池与工人进程

时间:2012-01-27 19:10:38

标签: python multiprocessing

我正在尝试使用Process对象在python中使用worker Pool。每个工作者(一个进程)进行一些初始化(花费非常重要的时间),传递一系列作业(理想情况下使用map()),并返回一些东西。除此之外不需要任何沟通。但是,我似乎无法弄清楚如何使用map()来使用我的worker compute()函数。

from multiprocessing import Pool, Process

class Worker(Process):
    def __init__(self):
        print 'Worker started'
        # do some initialization here
        super(Worker, self).__init__()

    def compute(self, data):
        print 'Computing things!'
        return data * data

if __name__ == '__main__':
    # This works fine
    worker = Worker()
    print worker.compute(3)

    # workers get initialized fine
    pool = Pool(processes = 4,
                initializer = Worker)
    data = range(10)
    # How to use my worker pool?
    result = pool.map(compute, data)

作业队列是否可以排队,或者我可以使用map()吗?

3 个答案:

答案 0 :(得分:50)

我建议您使用队列。

class Worker(Process):
    def __init__(self, queue):
        super(Worker, self).__init__()
        self.queue = queue

    def run(self):
        print('Worker started')
        # do some initialization here

        print('Computing things!')
        for data in iter(self.queue.get, None):
            # Use data

现在你可以开始一堆这些,所有这些都是从一个队列中获得工作

request_queue = Queue()
for i in range(4):
    Worker(request_queue).start()
for data in the_real_source:
    request_queue.put(data)
# Sentinel objects to allow clean shutdown: 1 per worker.
for i in range(4):
    request_queue.put(None) 

这种事情应该允许你分摊多个工人的昂贵的启动成本。

答案 1 :(得分:5)

initializer期望一个可以执行启动的任意调用,例如,它可以设置一些全局变量,而不是Process子类; map接受任意迭代:

#!/usr/bin/env python
import multiprocessing as mp

def init(val):
    print('do some initialization here')

def compute(data):
    print('Computing things!')
    return data * data

def produce_data():
    yield -100
    for i in range(10):
        yield i
    yield 100

if __name__=="__main__":
  p = mp.Pool(initializer=init, initargs=('arg',))
  print(p.map(compute, produce_data()))

答案 2 :(得分:1)

从python 3.3开始,您可以使用starmap,也可以使用非常简单的语法使用多个参数并取回结果

import multiprocessing

nb_cores = multiprocessing.cpu_count()

def caps(nb, letter):
    print('Exec nb:', nb)
    return letter.upper()

if __name__ == '__main__':

    multiprocessing.freeze_support() # for Windows, also requires to be in the statement: if __name__ == '__main__'

    input_data = ['a','b','c','d','e','f','g','h']
    input_order = [1,2,3,4,5,6,7,8,9]

    with multiprocessing.Pool(processes=nb_cores) as pool: # auto closing workers
        results = pool.starmap(caps, zip(input_order, input_data))

    print(results)