【问题标题】:Parallel/multiprocessing (nested) loops in Python and storing the results?Python中的并行/多处理(嵌套)循环并存储结果?
【发布时间】:2020-07-09 21:09:00
【问题描述】:

我有两个问题,我认为它们相关性足以成为一个问题的一部分。但如果不是,我可以将它们作为单独的问题提出。请告诉我。我也提前道歉,因为我觉得我做错了什么,但我不知道是什么。

到目前为止,我在 Python 中运行以下代码(使用 Jupyter 笔记本,如果有影响的话):

首先,我初始化一个很长的(多级?)列表:

object = [[[[[[[[None for i in range(2)]  
               for j in range(2)] 
              for k in range(2)] 
             for l in range (2)] 
            for m in range (2)] 
           for n in range (2)]
          for o in range (2)]
         for p in range (2)]

接下来,我运行一堆循环,一个在另一个循环中,然后运行一个函数(这取决于我在循环中使用的所有索引),将结果分配给我在上面创建的一个位置:

for i in range(2):
    for j in range(2):
        for k in range(2):
            for l in range(2):
                for m in range(2):
                    for n in range(2):
                        for o in range(2):
                            for p in range(2):
                                object[i][j][k][l][m][n][o][p] = function(i,j,k,l,m,n,o,p) 

这是两个相关的问题:

  1. 函数在每次迭代中返回的对象彼此完全独立(例如,我可以在一台计算机上运行循环的每次迭代并稍后收集它们)。所以我想这个循环将是在并行/多处理中解决的理想候选者。如果是这样,我该怎么做?我发现一些提到并行运行嵌套循环,但我不明白它如何适用于我的案例。完全披露:我从来没有在 Python 中并行运行过任何东西。

  2. 这个列表(引用这个相当令人不快的object[i][j][k][l][m][n][o][p])是如何正确保留结果的(以您以后可以找到的方式)?或者你能建议一个更好的方法吗?如果相关,则函数返回的对象具有 pandas 数据帧、数字和字符串等属性。

【问题讨论】:

  • 如果您的案例仅适用于 2**8 并且您不太关心速度,那么您当前的代码对我来说看起来不错。 1)当使用多处理时,就像一个 CPU 做 i=0,另一个做 i=1 部分。见multiprocessing doc。 2) 似乎与 object[][][]... 妥善保存

标签: python loops indexing multiprocessing


【解决方案1】:

对于您的第一个问题,我建议您在此处查看最佳答案,以了解如何并行化我在下面概述的 for 循环(它回答了问题 2):

How do I parallelize a simple Python loop?

第二个问题:

#dummy function for illustrative purposes
def function(a,b,c,d,e,f,g,h):
  return a+b+c+d+e+f+g+h

如果函数的输出是可散列的,我会创建一个字典:

#This is your 'objects'
O={}

for y in range(2**8):
    #this generates all the permutations you were after I believe
    s=format(y, '#010b')[2:]
    #print(s) #uncomment to see what it does
    #This is slightly messy, in that you have to split up your integer into its components, but I've seen worse.
    O[y]=function(int(s[0]),int(s[1]),int(s[2]),int(s[3]),int(s[4]),int(s[5]),int(s[6]),int(s[7]))

#Now, if you wanted to print the output of f(1,1,1,1,1,1,1,1):
g='11111111'
print(O[int(g,2)]) #uncomment to see what it does 

#print(O) #uncomment to see what it does 

如果输出不可散列,则保留为列表:

O=[] 

for y in range(2**8):
    #this generates all the permutations you were after I believe
    s=format(y, '#010b')[2:]
    #print(s) #uncomment to see what it does
    #This is slightly messy, in that you have to split up your integer into its components, but I've seen worse.
    O.append(function(int(s[0]),int(s[1]),int(s[2]),int(s[3]),int(s[4]),int(s[5]),int(s[6]),int(s[7])))

#Now, if you wanted to print the output of f(1,1,1,1,1,1,1,1):
g='11111111'
#print(O[int(g,2)]) #uncomment to see what it does 

#print(O) #uncomment to see what it does 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多