多处理无法腌制顶级函数



我正在尝试并行化我的任务,并决定使用多处理(mp(模块。 我很快就知道mp不能处理嵌套函数,所以我不得不取消嵌套一个类似于这样的函数:

def foo(nomer1, nomer2):
def bar(nomer1, nomer2):
return ([nomer1] * nomer2)
return(bar(nomer1, nomer2))

对此:

def bar(nomer1, nomer2):
return([nomer1]*nomer2)
def foo(nomer1, nomer2):
return(bar(nomer1, nomer2))

但是,当我运行时:

import multiprocessing as mp
n_proc = 2
pool = mp.Pool(processes=n_proc)
pool.starmap(foo, [11, 2])

控制台会产生此错误:

Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:UsersuserAnaconda2envsenv1libmultiprocessingpool.py", line 274, in starmap
return self._map_async(func, iterable, starmapstar, chunksize).get()
File "C:UsersuserAnaconda2envsenv1libmultiprocessingpool.py", line 644, in get
raise self._value
File "C:UsersuserAnaconda2envsenv1libmultiprocessingpool.py", line 424, in _handle_tasks
put(task)
File "C:UsersuserAnaconda2envsenv1libmultiprocessingconnection.py", line 206, in send
self._send_bytes(_ForkingPickler.dumps(obj))
File "C:UsersuserAnaconda2envsenv1libmultiprocessingreduction.py", line 51, in dumps
cls(buf, protocol).dump(obj)
_pickle.PicklingError: Can't pickle <function foo at 0x00000189D37902F0>: attribute lookup foo on __main__ failed

Foo在最顶层,如文档中指定的那样,但MP仍然无法腌制它。 我该如何解决这个问题,最好不要安装额外的软件包?

我已经解决了这个问题,使用concurrent.futures
这个包应该与我的 Python 安装一起使用,并且问题很容易解决:

pool = ThreadPoolExecutor(2)
future = pool.submit(foo, 11,2)
print(future.result()) 

该错误似乎是已知错误的结果。不过,建议的Anaconda更新无济于事。我想知道这个错误在被报告一年后是否仍然存在。

与此同时,future的ProcessPoolExecutor产生了类似的错误:

Traceback (most recent call last):
File "C:UsersuserAnaconda2envsenv1libmultiprocessingqueues.py", line 234, in _feed
obj = _ForkingPickler.dumps(obj)
File "C:UsersuserAnaconda2envsenv1libmultiprocessingreduction.py", line 51, in dumps
cls(buf, protocol).dump(obj)
_pickle.PicklingError: Can't pickle <function foo at 0x000001D816CF5510>: attribute lookup foo on __main__ failed

相关内容

  • 没有找到相关文章

最新更新