I want to apply this function in multiprocessing:
import multiprocessing
import numpy as np
a = np.random.randint(1,4, size=(10,10,10))
def function(input_field,[num1,num2,num3]):
pass
if __name__ == '__main__':
pool = multiprocessing.Pool(processes=4)
test_list = [(a,[27.5, 27.5, 25]), (b,[27.5, 27.5, 25]), (c,[27.5, 27.5, 25]), (d,[27.5, 27.5, 25])]
results = pool.map(function, test_list)
pool.close()
pool.join()
How can I apply function(input_field,[num1,num2,num3])
to multiprocessing?
You have 2 options based on the structure of your test_list
:
modify your function to support a single argument and unpack it inside the function and keep using map
:
def function(arg): input_field, l = arg
use starmap
and fix your function definition, because the structure of test_list
is what starmap
is expecting to get:
def function(input_field, l): ... results = pool.starmap(function, test_list)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.