在我的程序中,我需要在Python的多进程之间共享一个字典。我将代码简化,以便在此处提供示例:
import multiprocessing
def folding (return_dict, seq):
dis = 1
d = 0
ddg = 1
'''This is irrelevant, actually my program sends seq parameter to other extern program that returns dis, d and ddg parameters'''
return_dict [seq] = [dis, d, ddg]
seqs = ['atcgtg', 'agcgatcg', 'atcgatcgatc', atcggatcg', agctgctagct']
manager = Manager()
return_dict = manager.dict()
n_cores = 3
for i in range (0, len(seqs), n_cores) #n_cores is the number of cores availables in the computer, defined by the user
subseqs = seqs[i:i + n_cores]
processes = [Process(target=folding, args =(return_dict, seq)) for seq in subseqs]
for p in processes:
p.start()
for p in processes:
p.join()
for i in retun_dict:
print i
我希望在程序结束时能够得到包含所有属性值的return_dict。运行程序时,需要处理数千个序列并多次重复操作。有时可以得到正确的结果,但大部分情况下程序无法结束,也没有任何错误提示,我不知道出了什么问题。此外,我认为这种方法在时间上并不是很有效率,想知道是否有更快更高效的方式来实现。谢谢大家!