gpt4 book ai didi

python - 将 Pool.map 与列表 : TypeError: '<=' not supported between instances of 'list' and 'int' 一起使用

转载 作者:行者123 更新时间:2023-12-02 02:31:34 31 4
gpt4 key购买 nike

我正在尝试加速我的代码,所以我使用:

pool = Pool(cpu_count()-1)
print('Start multi process')
res = pool.map(max_def_in_circle, range(len(dataT)), All_index_in_c)

with All_index_in_c 是列表的列表:这里是预览

[[2, 12], [11, 25, 26, 27, 28, 29], [0, 3, 12], [], [21, 22, 44, 45, 46], .... ]

max_def_in_circle 一个非常基本的函数,运行良好

但是当我运行它时,我得到了这个错误信息:

<ipython-input-18-3bd316855b1c> in <module>
6 # pool.join()
7 print('Start multi process')
----> 8 res = pool.map(max_def_in_circle, range(len(dataT)), All_index_in_c)

/usr/lib/python3.8/multiprocessing/pool.py in map(self, func, iterable, chunksize)
362 in a list that is returned.
363 '''
--> 364 return self._map_async(func, iterable, mapstar, chunksize).get()
365
366 def starmap(self, func, iterable, chunksize=None):

/usr/lib/python3.8/multiprocessing/pool.py in _map_async(self, func, iterable, mapper, chunksize, callback, error_callback)
483
484 task_batches = Pool._get_tasks(func, iterable, chunksize)
--> 485 result = MapResult(self, chunksize, len(iterable), callback,
486 error_callback=error_callback)
487 self._taskqueue.put(

/usr/lib/python3.8/multiprocessing/pool.py in __init__(self, pool, chunksize, length, callback, error_callback)
795 self._value = [None] * length
796 self._chunksize = chunksize
--> 797 if chunksize <= 0:
798 self._number_left = 0
799 self._event.set()

TypeError: '<=' not supported between instances of 'list' and 'int'

但是当我搜索这个问题时,几个人似乎可以在 pool.map 中使用列表我不明白为什么,也许是版本问题?如果有人可以帮助我,谢谢

最佳答案

正如@DarrylG 提到的,pool.map 仅支持一个参数的函数。使用 pool.starmap 将多个参数传递给 max_def_in_circle,例如

res = pool.starmap(max_def_in_circle, zip(range(len(dataT)), All_index_in_c))

zip 函数会将每个列表中的条目成对输出。参见 here了解详情。

关于python - 将 Pool.map 与列表 : TypeError: '<=' not supported between instances of 'list' and 'int' 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64967103/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com