我需要并行学习来加快速度。我对Python的多进程库不熟悉,尚未成功运行。
现在,我正在研究每个(源,目标)对是否在我的学习的各个帧之间的某些位置上保持不变。以下是几点:
- 这是一个函数,我想要使其运行更快(不是多个进程)。
- 该过程是顺序执行的,也就是说每一帧都与前一帧进行比较。
- 此代码是原始代码的简化形式。 代码输出一个residence_list。
- 我在使用Windows操作系统。
有人可以检查代码(多进程部分)并帮助我改进它以使其正常工作吗? 谢谢。
import numpy as np
from multiprocessing import Pool, freeze_support
def Main_Residence(total_frames, origin_list, target_list):
Previous_List = {}
residence_list = []
for frame in range(total_frames): #Each frame
Current_List = {} #Dict of pair and their residence for frames
for origin in range(origin_list):
for target in range(target_list):
Pair = (origin, target) #Eahc pair
if Pair in Current_List.keys(): #If already considered, continue
continue
else:
if origin == target:
if (Pair in Previous_List.keys()): #If remained from the previous frame, add residence
print "Origin_Target remained: ", Pair
Current_List[Pair] = (Previous_List[Pair] + 1)
else: #If new, add it to the current
Current_List[Pair] = 1
for pair in Previous_List.keys(): #Add those that exited from residence to the list
if pair not in Current_List.keys():
residence_list.append(Previous_List[pair])
Previous_List = Current_List
return residence_list
if __name__ == '__main__':
pool = Pool(processes=5)
Residence_List = pool.apply_async(Main_Residence, args=(20, 50, 50))
print Residence_List.get(timeout=1)
pool.close()
pool.join()
freeze_support()
Residence_List = np.array(Residence_List) * 5
multiprocessing.Pool
的问题这里。希望你能抽出一些时间来检查一下这个问题。非常感谢你的帮助! - Akira