python process参数_如何将具有多个参数的函数传递给python concurrent.futures.ProcessPoolExecutor.map()?...

我想concurrent.futures.ProcessPoolExecutor.map()来调用一个由2个或更多参数组成的函数.在下面的示例中,我使用了lambda函数并将ref定义为具有相同值的numberlist大小相等的数组.

第一个问题:有更好的方法吗?在numberlist的大小可能是数百万到数十亿个元素的情况下,因此ref大小必须遵循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.futures.ProcessPoolExecutor.map()时,python3.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 at 0x7fd2a14db0d0>: attribute lookup on __main__ failed

问题2:为什么会出现此错误?如何使用concurrent.futures.ProcessPoolExecutor.map()调用具有多个参数的函数?

解决方法:

要先回答你的第二个问题,你会得到一个例外,因为你正在使用的lambda函数是不可选的.由于Python使用pickle协议来序列化主进程和ProcessPoolExecutor的工作进程之间传递的数据,因此这是一个问题.目前尚不清楚你为什么要使用lambda.你拥有的lambda有两个参数,就像原始函数一样.您可以直接使用_findmatch而不是lambda,它应该可以工作.

with cf.ProcessPoolExecutor(max_workers=workers) as executor:

for n in executor.map(_findmatch, numberlist, ref):

...

关于传递第二个常量参数而不创建巨型列表的第一个问题,您可以通过多种方式解决这个问题.一种方法可能是使用itertools.repeat创建一个可迭代的对象,在迭代时永远重复相同的值.

但是更好的方法可能是编写一个额外的函数来为你传递常量参数. (也许这就是你尝试使用lambda函数的原因?)如果您使用的函数可以在模块的顶级命名空间访问,它应该可以工作:

def _helper(x):

return _findmatch(x, 5)

with cf.ProcessPoolExecutor(max_workers=workers) as executor:

for n in executor.map(_helper, numberlist):

...

标签:concurrent-futures,python,lambda,python-3-x,concurrency

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值