如何并行化复杂的for循环

时间:2019-02-28 14:01:57

标签: python for-loop parallel-processing

我有一个复杂的for循环,其中包含一个循环中多个记录的多个操作。循环看起来像这样:

for i,j,k in zip(is,js,ks):
    #declare multiple lists.. like
    a = []
    b = []
    #...
    if i:
        for items in i:
            values = items['key'].split("--")
            #append the values to the declared lists
            a.append(values[0])
            b.append(values[1])
    # also other operations with j and k where are is a list of dicts. 
    if "substring" in k:
        for k, v in j["key"].items():
            l = "string"
            t = v
    else:
        for k, v in j["key2"].items():
            l = k
            t = v

            # construct an object with all the lists/params
            content = {
                'sub_content': {
                    "a":a,
                    "b":b,
                    .
                    .
                }
            }

            #form a tuple. We are interested in this tuple.
            data_tuple = (content,t,l)

考虑上述for循环,如何并行化?我已经研究了多处理,但无法并行化如此复杂的循环。我也欢迎可能在这里表现更好的建议,包括OpenMP / MPI / OpenACC等并行语言范例。

1 个答案:

答案 0 :(得分:2)

您可以使用Python multiprocessing库。如this excellent answer中所述,您应该确定是否需要多处理或多线程。

底线:如果需要多线程,则应使用multiprocessing.dummy。如果仅执行没有IO /依赖关系的CPU密集型任务,则可以使用多处理。

  

multiprocessing.dummy与多处理模块完全相同,   但改用线程(一个重要的区别-使用多个   CPU密集型任务的流程; (以及在IO期间)的线程数:

设置zip对象

#!/usr/bin/env python3

import numpy as np

n = 2000
xs = np.arange(n)
ys = np.arange(n) * 2
zs = np.arange(n) * 3

zip_obj = zip(xs, ys, zs)

简单的示例功能

def my_function(my_tuple):
    iv, jv, kv = my_tuple
    return f"{str(iv)}-{str(jv)}-{str(kv)}"   

设置多线程。

from multiprocessing.dummy import Pool as ThreadPool
pool = ThreadPool(4)
data_tuple = pool.map(my_function, zip_obj)

您的完整示例

def my_function(my_tuple):
    i, j, k = my_tuple
    #declare multiple lists.. like
    a = []
    b = []
    #...
    if (i):
        for items in i:
            values = items['key'].split("--")
            #append the values to the declared lists
            a.append(values[0])
            b.append(values[1])
     #also other ooperations with j and k where are is a list of dicts. 
     if ("substring" in k):
           for k, v in j["key"].items():
               l = "string"
               t = v
      else:
           for k, v in j["key2"].items():
               l = k
               t = v
    #construct an object called content with all the lists/params like
           content = {
                'sub_content': {
                  "a":a,
                  "b":b,
                  .
                  .
                }
            }
    #form a tuple. We are interested in this tuple.
    return (content,t,l)


from multiprocessing.dummy import Pool as ThreadPool
pool = ThreadPool(4)
zip_obj = zip(is,js,ks)
data_tuple = pool.map(my_function, zip_obj)
# Do whatever you need to do w/ data_tuple here