简体   繁体   中英

How can I use this function in multiprocessing?

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 :

  1. 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
  2. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM