最佳答案
我需要一些方法来使用 pool.map ()中接受多个参数的函数。根据我的理解,pool.map ()的目标函数只能有一个可迭代的参数,但是有没有一种方法可以传递其他参数呢?在这种情况下,我需要向目标函数传入一些配置变量,比如 Lock ()和日志信息。
我已经尝试做了一些研究,我认为我可以使用部分函数来使它工作?然而,我不完全理解这些工作原理。任何帮助都将不胜感激!下面是我想做的一个简单的例子:
def target(items, lock):
for item in items:
# Do cool stuff
if (... some condition here ...):
lock.acquire()
# Write to stdout or logfile, etc.
lock.release()
def main():
iterable = [1, 2, 3, 4, 5]
pool = multiprocessing.Pool()
pool.map(target(PASS PARAMS HERE), iterable)
pool.close()
pool.join()