1

我很难理解 Python 多处理模块中池的用途。

我知道这段代码在做什么:

import multiprocessing

def worker():
    """worker function"""
    print 'Worker'
    return

if __name__ == '__main__':
    jobs = []
    for i in range(5):
        p = multiprocessing.Process(target=worker)
        jobs.append(p)
        p.start()

所以我的问题是,在什么情况下会使用池?

4

1 回答 1

2

Pool objects are useful when you want to be able to submit more tasks to sub-processes, but you don't want to handle all the organization of these tasks(i.e. how many processes should be spawned to handle them; which task go to which process etc.) and you care only for the result value, and not any other kind of synchronisation etc. You don't want to have the control over the sub-process computation but simply the result.

On the other hand Process is used when you want to execute a specific action, and you need control over the sub-process, not only on the result of its computation.

于 2013-06-11T20:44:37.433 回答