如何将仅关键字的参数应用于多处理池中的函数?

问题描述 投票:0回答:1

我有一个只接受关键字参数的函数,希望在进程池中运行它。如何将我的条目从可迭代对象作为关键字参数传递给流程中的函数?

import multiprocessing

greetees = ('Foo', 'Bar')

def greet(*, greetee):
    return f'Hello, {greetee}!'

我尝试使用multiprocessing.map:

greetings = multiprocessing.Pool(2).map(greet, greetees)
print(list(greetings))

但是,这引起了异常,正如预期的那样:

multiprocessing.pool.RemoteTraceback: 
"""
Traceback (most recent call last):
  File "/usr/lib/python3.6/multiprocessing/pool.py", line 119, in worker
    result = (True, func(*args, **kwds))
  File "/usr/lib/python3.6/multiprocessing/pool.py", line 44, in mapstar
    return list(map(*args))
TypeError: greet() takes 0 positional arguments but 1 was given
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/bengt/Projekte/gitlab.com/PFASDR/PFASDR.Code.Main/pfasdr/neural/multi_pool_kwargs.py", line 10, in <module>
    greetings = multiprocessing.Pool(2).map(greet, greetees)
  File "/usr/lib/python3.6/multiprocessing/pool.py", line 266, in map
    return self._map_async(func, iterable, mapstar, chunksize).get()
  File "/usr/lib/python3.6/multiprocessing/pool.py", line 644, in get
    raise self._value
TypeError: greet() takes 0 positional arguments but 1 was given
python dictionary multiprocessing pool
1个回答
0
投票

这里的解决方案是使用Pool.applyPool.apply_async

greetings = (
    multiprocessing.Pool(2).apply(greet, kwds={'greetee': greetees[i]})
    for i in range(len(greetees))
)
print(list(greetings))

输出:

['Hello, Foo!', 'Hello, Bar!']
© www.soinside.com 2019 - 2024. All rights reserved.