I am trying to figure out how to do multithreading in Python, namely how to use the map function to call my function with multiple variables.
My code:
from multiprocessing.dummy import Pool as ThreadPool
def myFunc(a, b):
print(a * b)
arr = [4, 8, 10, 7, 9]
arr2 = [2, 1, 3, 4, 2]
pool = ThreadPool(4)
results = pool.map(myFunc, arr, arr2)
pool.close()
pool.join()
It get the following error from the pool.map function call:
TypeError: '<=' not supported between instances of 'list' and 'int'
Which I don't quite get. From the documentation I should be able to do this.
What am I missing?
Here is an example. The built-in map
can take more than two iterators, but for pool.map
, it can take only one iterator as the second argument.
from multiprocessing import Pool
def myFunc(x):
print(x[0] * x[1])
arr = [4, 8, 10, 7, 9]
arr2 =[2, 1, 3, 4, 2]
if __name__ == '__main__':
pool = Pool(4)
results = pool.map(myFunc, zip(arr, arr2))
pool.close()
pool.join()
Please compare the followings for more detail.
https://docs.python.org/3.6/library/multiprocessing.html#multiprocessing.pool.Pool.map https://docs.python.org/3/library/functions.html#map
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With