简体   繁体   English

池在python多处理中的用途

[英]The purpose of the pool in python multiprocessing

I'm having difficulty understanding the purpose of the pool in Python's multiprocessing module. 我很难理解Python的多处理模块中池的用途。

I know what this code is doing: 我知道这段代码在做什么:

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()

So my question is, in what type of situation would a pool be used? 所以我的问题是,在哪种情况下将使用池?

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(ie 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. 当您希望能够向子流程提交更多任务,但又不想处理这些任务的所有组织时(即应产生多少个流程来处理它们,哪个任务去往哪个对象, Pool对象就很有用)处理等),而您只关心结果值,而不关心任何其他类型的同步等。您不想控制子过程的计算,而只希望控制结果。

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. 在另一方面Process ,当你想执行一个特定的动作时,你需要控制的子过程中,不仅在其计算的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM