多处理器数字加法器

Multiprocessor Numerical Adder

所以我目前正在参与一个大学项目,该项目正在研究癌症患者的数千个基因数据样本,可能程序花费的时间太长 运行 所以我使用了多处理,它在apple mac 我的朋友借了我,但是当我把它转移到大学 windows 系统时它就失败了,我不确定为什么这个程序不再工作了。

我决定尽可能简单地剥离我的代码以查看错误,我的程序本身没有多处理元素来加速样本数量工作正常。我相信问题围绕着下面的代码。我没有放置很长的程序,而是将其切换为简单的加法,但它仍然不起作用,使用了非常高的 cpu 并且我看不出我哪里出错了。亲切的问候。

预期结果是瞬间5、15、25、35,我的电脑上有windows 10 我目前正在使用。


 import multiprocessing
 from multiprocessing import Pool
 import collections
 value=collections.namedtuple('value',['vectx','vecty'])
 Values=(value(vectx=0,vecty=5),value(vectx=5,vecty=10),value(vectx=10,vecty=15),value(vectx=15,vecty=20))
 print(1)
 def Alter(x):
   vectx=x.vectx
   vecty=x.vecty
   Z=(vectx+vecty)
   return(Z)
 if __name__ == '__main__':
     with Pool(2) as p:
          result=p.map(Alter, Values)
 print(2)
 new=[]
 for i in result:
     new.append(i)
 print(new) 

我不知道为什么但是这部分

 print(2)
 new=[]
 for i in result:
     new.append(i)
 print(new) 

需要if语句的组中。类似于 the documentation.

中的示例
if __name__ == '__main__':
    with Pool(2) as p:
        result=p.map(Alter, Values)
    print(2)
    new=[]
    for i in result:
        new.append(i)
    print(new) 

我怀疑 - Compulsory usage of if __name__==“__main__” in windows while using multiprocessing - 可能相关。

如果您运行您的原始代码来自命令shell(如PowerShell或命令提示符)python -m mymodulename您将看到所有正在进行的 stuff - 来自多个派生进程的回溯。