Python Can';t在multiprocessing.pool.IMapIterator对象上迭代

Python Can';t在multiprocessing.pool.IMapIterator对象上迭代,python,python-3.x,list,iterator,multiprocessing,Python,Python 3.x,List,Iterator,Multiprocessing,不确定它是否相关,但我使用Spyder(版本3.3.3)作为Python IDE,这个Python版本是3.7.3 以下是我的代码: import os import numpy as np import multiprocessing as mp from itertools import product def func1(a_tup): return (a_tup[0][0], *a_tup[1]), (a_tup[0][1]+a_tup[1][0])*a_tup[1][1]

不确定它是否相关,但我使用Spyder(版本3.3.3)作为Python IDE,这个Python版本是3.7.3

以下是我的代码:

import os
import numpy as np
import multiprocessing as mp

from itertools import product

def func1(a_tup):
    return (a_tup[0][0], *a_tup[1]), (a_tup[0][1]+a_tup[1][0])*a_tup[1][1]

if __name__ == '__main__':

    lst1 = np.arange(4)
    lst2 = np.arange(2, 8, 3)
    lst3 = product(enumerate(lst2),
                      (item for item in product(lst1, lst1) if item[0] < item[1])
                      )

    result1 = np.zeros((len(lst2), len(lst1) -1, len(lst1)), dtype = np.float32)

    with mp.Pool(os.cpu_count() - 1) as pool:
        mp_result1 = pool.imap(func1, lst3)#<--this line right here

    for item in mp_result1:
        print(item)
导入操作系统
将numpy作为np导入
将多处理作为mp导入
来自itertools进口产品
def func1(安装程序):
返回(a_-tup[0][0],*a_-tup[1]),(a_-tup[0][1]+a_-tup[1][0])*a_-tup[1][1]
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
lst1=np.arange(4)
lst2=np.arange(2,8,3)
lst3=产品(枚举(lst2),
(如果项目[0]<项目[1],则产品(lst1,lst1)中项目的项目)
)
result1=np.zero((len(lst2),len(lst1)-1,len(lst1)),dtype=np.float32)
使用mp.Pool(os.cpu\u count()-1)作为池:

mp_result1=pool.imap(func1,lst3)#尝试将
for
循环移动到
with
块中-在池进程被终止(退出池上下文管理器即可)后尝试使用
mp_result1
是没有意义的。这确实解决了我的问题。这对我来说很奇怪,因为当行是
mp_result1=list(pool.imap(func1,lst3))
时,将循环带到外部不会使脚本无法运行。只要
list(…)
行在
with
块中,这是意料之中的。该列表在
with
块退出(并在退出时杀死池进程)之前完全创建。。。谢谢你的回复。我不知道在多重处理中是否有任何地方可以查找这些行为。唉,没有任何有用的“这些行为”可供查找的概括-你唯一能做的就是询问当时让你困惑的具体行为;-)Parallel processomg充满了微妙之处,即使在
多处理
包的高级别上也是如此。请尝试将
for
循环移动到
with
块中-在池进程被终止后尝试使用
mp_result1
(退出池上下文管理器即可).这确实解决了我的问题。这对我来说很奇怪,因为当行是
mp_result1=list(pool.imap(func1,lst3))
时,将循环带到外部不会使脚本无法运行。只要
list(…)
行在
with
块中,这是意料之中的。该列表在
with
块退出(并在退出时杀死池进程)之前完全创建。。。谢谢你的回复。我不知道在多重处理中是否有任何地方可以查找这些行为。唉,没有任何有用的“这些行为”可供查找的概括-你唯一能做的就是询问当时让你困惑的具体行为;-)并行处理OMG充满了微妙之处,即使是在
多处理
包的高级。