多进程分配数组任务,保存对应数组下标到字典中。
from multiprocessing import Pool, Manager, cpu_count
import numpy as np
import time
def func1(i, j, segment_positions_dict):
# wait 100s
# time.sleep(0.5)
segment_positions_dict[(i, j)] = i + j # 存储索引和value
def way2():
mat = np.zeros((10, 10)).tolist()
manager = Manager()
segment_positions_dict = manager.dict() # 多进程共享字典
pool = Pool(processes=3)
print('start')
for i in range(10):
for j in range(i+1, 10):
print(f'join {i} {j}')
pool.apply_async(func1, (i, j, segment_positions_dict,))
print('end')
pool.close()
pool.join()
print('all done')
# print(segment_positions_dict)
for (i, j), xcorr in segment_positions_dict.items():
mat[i][j] = xcorr
mat[j][i] = xcorr
print(mat)
if __name__ == '__main__':
way2()