Python 工作进程池和只运行多个进程有什么区别?

标签 python process multiprocessing pool worker

我不确定何时使用工作池与多进程。

processes = []

for m in range(1,5):
       p = Process(target=some_function)
       p.start()
       processes.append(p)

for p in processes:
       p.join()

对比

if __name__ == '__main__':
    # start 4 worker processes
    with Pool(processes=4) as pool:
        pool_outputs = pool.map(another_function, inputs)

最佳答案

如其所说on PYMOTW :

The Pool class can be used to manage a fixed number of workers for simple cases where the work to be done can be broken up and distributed between workers independently.

The return values from the jobs are collected and returned as a list.

The pool arguments include the number of processes and a function to run when starting the task process (invoked once per child).

请查看此处给出的示例,以更好地了解其应用、功能和参数。

基本上,Pool 是一个 helper ,在他们需要做的就是使用公共(public)输入数据、并行处理它并产生联合输出的情况下,简化流程(工作人员)的管理。

Pool 做了很多事情,否则你应该自己编写代码(不是太难,但仍然很方便找到预煮的解决方案)

  • 输入数据的拆分
  • 目标流程功能被简化:它可以被设计成只需要一个输入元素。 Pool 将调用它来提供分配给该工作人员的子集中的每个元素
  • 等待 worker 完成工作(即加入流程)
  • ...
  • 合并每个 worker 的输出以产生最终输出

关于Python 工作进程池和只运行多个进程有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33445603/

相关文章:

当 for 循环工作时,Python 多重处理不工作

python - 如何修复 : "google.protobuf.message.DecodeError: Error parsing message" when creating tensorflow text summary

c++ - 打开具有调试权限和读/写内存的进程

session - 退出 SSH session 后保留 sbt 进程的最合适方法?

python - Pandas 简单的并行/多进程计算

python - 如何将生成器用作具有多处理映射函数的可迭代对象

python - 用不同的词替换文本文件中的词(python)

Python 惰性求值 numpy ndarray

python - 使用 Pandas 动态创建数据框

windows - 为程序提供所有可能的资源