本文介绍了具有不同功能的多进程池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
多进程工作池的大多数示例在不同的进程中执行单个功能,例如
Most examples of the Multiprocess Worker Pools execute a single function in different processes, f.e.
def foo(args):
pass
if __name__ == '__main__':
pool = multiprocessing.Pool(processes=30)
res=pool.map_async(foo,args)
是否有一种方法可以处理池中的两个不同且独立的功能?这样您就可以分配f.e. foo()的15个进程和bar()的15个进程还是池绑定到单个函数?或者,您必须使用以下方法手动为不同的功能创建不同的过程
Is there a way to handle two different and independent functions within the pool? So that you could assign f.e. 15 processes for foo() and 15 processes for bar() or is a pool bounded to a single function? Or du you have to create different processes for different functions manually with
p = Process(target=foo, args=(whatever,))
q = Process(target=bar, args=(whatever,))
q.start()
p.start()
忘记工作人员池了吗?
推荐答案
要传递不同的功能,只需多次调用map_async
.
To pass different functions, you can simply call map_async
multiple times.
这里是一个例子来说明这一点,
Here is an example to illustrate that,
from multiprocessing import Pool
from time import sleep
def square(x):
return x * x
def cube(y):
return y * y * y
pool = Pool(processes=20)
result_squares = pool.map_async(f, range(10))
result_cubes = pool.map_async(g, range(10))
结果将是:
>>> print result_squares.get(timeout=1)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> print result_cubes.get(timeout=1)
[0, 1, 8, 27, 64, 125, 216, 343, 512, 729]
这篇关于具有不同功能的多进程池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!