I want to get the outputs by using pool and map function in python like this:
**Output in random order:
PoolWorker-10 output: [0, 4, 8, 12, 16, 20]
PoolWorker-11 output: [0, 4, 8, 12, 16, 20]
PoolWorker-12 output: [0, 4, 8, 12, 16, 20]
PoolWorker-1 output: [0, 4, 8, 12, 16, 20]
.....**
I tried several times to write the output result and current_process name together in line but I failed it. I don't know which should be changed in order to get the printed output like that above.
from multiprocessing import Pool, current_process
def multiprocessing_func():
input_list = [0, 1, 2, 3, 4, 5]
print("input list: {}".format(input_list))
print("Output in random order: ")
pool = Pool(10, current_process_func)
result = pool.map(calculate, input_list)
print(result)
pool.close()
pool.join()
def calculate(y_list):
return y_list * 4
def current_process_func():
print("{} output: ".format(current_process().name))
if __name__ == '__main__':
multiprocessing_func()
When I run it, I get the result like this below:
Output in random order:
PoolWorker-1 output:
PoolWorker-2 output:
PoolWorker-3 output:
PoolWorker-4 output:
PoolWorker-5 output:
PoolWorker-7 output:
PoolWorker-6 output:
[0, 4, 8, 12, 16, 20]
PoolWorker-8 output:
PoolWorker-9 output:
PoolWorker-10 output:
What should I change to get the result that I want above?