不能在0x104144f28>处腌制<函数字符串ToNgrams:它与__main__.stringToNgrams不是同一个对象



我正在尝试使用多处理来对对象列表运行函数。但是,此特定函数不属于类或模块 - 它直接在多处理代码上方定义。

这是我的代码:

def stringToNgrams(string,n):
ngrams = set()
sentenceArray = string.split(" ")
for i in range(0,len(sentenceArray) - (n - 1)):
ngramWords = sentenceArray[i:i + n]
print(ngramWords)
ngram = " ".join(ngramWords)
print(ngram)
ngrams.add(ngram)
from multiprocessing import Pool
ngramPool = Pool(3)
import functools
stringToNgrams = functools.partial(stringToNgrams,n=2) # So we can load n into stringToNgrams.
results = ngramPool.map(stringToNgrams,strings)

这是我的错误消息:

File "/Users/montana/Documents/Capstone project/BBEditRunTemp-n_gram.py", line 76, in <module>
results = ngramPool.map(stringToNgrams,strings)
File "/Users/montana/miniconda3/lib/python3.6/multiprocessing/pool.py", line 260, in map
return self._map_async(func, iterable, mapstar, chunksize).get()
File "/Users/montana/miniconda3/lib/python3.6/multiprocessing/pool.py", line 608, in get
raise self._value
File "/Users/montana/miniconda3/lib/python3.6/multiprocessing/pool.py", line 385, in _handle_tasks
put(task)
File "/Users/montana/miniconda3/lib/python3.6/multiprocessing/connection.py", line 206, in send
self._send_bytes(_ForkingPickler.dumps(obj))
File "/Users/montana/miniconda3/lib/python3.6/multiprocessing/reduction.py", line 51, in dumps
cls(buf, protocol).dump(obj)
_pickle.PicklingError: Can't pickle <function stringToNgrams at 0x104144f28>: it's not the same object as __main__.stringToNgrams

我似乎不知道该怎么做 - 如果我使用 main.stringToNgrams,Python 就会抱怨没有定义">main"这个名字。请帮助我,谢谢!

看起来这是一个与 functools.partial 构造函数相关的问题。从 Python 错误跟踪器:

自由主义的函数工具部分 构造函数使得很难正确腌制部分实例。

因此,答案是做以下两件事之一:

1( 将 n 设为可选参数

2( 创建另一个函数,将 2 传递给原始函数。

相关内容

  • 没有找到相关文章

最新更新