functools.partial不适用于multiprocessing.Pool.map吗?

时间:2010-09-03 16:51:12

标签: python multiprocessing

我有简化的代码,如下所示:

run = functools.partial(run, grep=options.grep, print_only=options.print_only, force=options.force)

if not options.single and not options.print_only and options.n > 0:
    pool = multiprocessing.Pool(options.n)
    Map = pool.map
else: Map = map

for f in args:
    with open(f) as fh: Map(run, fh)

try:
    pool.close()
    pool.join()
except NameError: pass

当我在单进程模式下运行它时工作正常,但是失败并出现这样的错误

TypeError: type 'partial' takes at least one argument

通过多处理模块与长调用栈混合在一起。发生了什么事?

我正在使用python 2.6.1。

2 个答案:

答案 0 :(得分:4)

谷歌告诉我这是bug in Python;显然固定在Py3k。据推测,由于partial无法被挑选。

workaround

答案 1 :(得分:0)

它可以使用:

from multiprocessing.dummy import Pool as ThreadPool

def executeFunction(server,function):
    print "Server: %s and function: %s" % (server,function)


def executeParallel(servers,function, threads=10):
    pool = ThreadPool(threads)
    functionArguments=partial(executeFunction, function=function)
    results = pool.map(functionArguments, servers)
    pool.close()
    pool.join()
    return results

executeParallel(serverList, "Install")