我想调用一个包含两个或更多参数的函数.在下面的示例中,我使用了lambda
函数,并将ref
定义为一个大小与numberlist
相同、值相同的array.
1st Question:有更好的方法吗?在numberlist的大小可能是百万到十亿个元素的情况下,因此ref size必须遵循numberlist,这种方法不必要地占用宝贵的内存,我希望避免这种情况.我这样做是因为我读到map
函数将终止其映射,直到到达最短的数组末端.
import concurrent.futures as cf
nmax = 10
numberlist = range(nmax)
ref = [5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
workers = 3
def _findmatch(listnumber, ref):
print('def _findmatch(listnumber, ref):')
x=''
listnumber=str(listnumber)
ref = str(ref)
print('listnumber = {0} and ref = {1}'.format(listnumber, ref))
if ref in listnumber:
x = listnumber
print('x = {0}'.format(x))
return x
a = map(lambda x, y: _findmatch(x, y), numberlist, ref)
for n in a:
print(n)
if str(ref[0]) in n:
print('match')
with cf.ProcessPoolExecutor(max_workers=workers) as executor:
#for n in executor.map(_findmatch, numberlist):
for n in executor.map(lambda x, y: _findmatch(x, ref), numberlist, ref):
print(type(n))
print(n)
if str(ref[0]) in n:
print('match')
运行上面的代码,我发现map
函数能够实现我想要的结果.然而,当我把同样的条款转移到concurrent时.期货处理池执行器.map(),Python 3.5因以下错误而失败:
Traceback (most recent call last):
File "/usr/lib/python3.5/multiprocessing/queues.py", line 241, in _feed
obj = ForkingPickler.dumps(obj)
File "/usr/lib/python3.5/multiprocessing/reduction.py", line 50, in dumps
cls(buf, protocol).dump(obj)
_pickle.PicklingError: Can't pickle <function <lambda> at 0x7fd2a14db0d0>: attribute lookup <lambda> on __main__ failed
Question 2:为什么会发生此错误,以及如何获得并发.期货处理池执行器.map()调用具有多个参数的函数?