![](/img/trans.png)
[英]Using Pool.map with a list : TypeError: '<=' not supported between instances of 'list' and 'int'
[英]"TypeError: '<=' not supported between instances of 'list' and 'int'" occur while using multiprocessing.Pool()
from multiprocessing import Pool
def f(x,y):
return x*y
with Pool(4) as p:
print(p.map(f,[2,2,2],[3,4,5]))
消息如下:
TypeError Traceback (most recent call last) <ipython-input-25-e9ce49d0ccbf> in <module>
1 p = multiprocess.pool.Pool()
2 # result = p.map(for_multical,[N]*n,[k]*n)
----> 3 print(p.map(f,[2,2,2],[3,4,5]))
4 p.close()
5 p.join()
D:\software\python\lib\site-packages\multiprocess\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):
D:\software\python\lib\site-packages\multiprocess\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(
D:\software\python\lib\site-packages\multiprocess\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'
self._chunksize = chunksize
chunksize
似乎塊大小未分配。 這怎么可能發生? 如何解決這個問題?
包“多進程”也有同樣的問題。
Pool.map()與常規map()
) 的不同之處在於它只接受一個可迭代對象。 解決方案是使用.starmap()代替,但它需要其他格式的迭代,請參閱鏈接文檔。
print(p.starmap(f, zip([2,2,2],[3,4,5]) ))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.