I would like concurrent.futures.ProcessPoolExecutor.map() call a function consisting of 2 or more arguments. In the example below, I resorted to using the lambda function and defined ref as an array of equal size with numberlist with the same value.
1st question: Is there a better way to do this? In the case where the size of the list of numbers can be from a million to a billion elements, therefore, the size of the link should correspond to the list of numbers, this approach unnecessarily takes precious memory, which I would like to avoid. I did this because I read the map function to complete its display until the shortest end of the array is reached.
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 xa = 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')
By running the code above, I found that the map function was able to achieve the desired result. However, when I passed the same conditions to concurrent.futures.ProcessPoolExecutor.map (), python3.5 failed with this error:
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 . Why did this error occur and how do I get concurrent.futures.ProcessPoolExecutor.map () to call a function with more than one argument?