简体   繁体   English

如何在 Python 中并行化 for 循环?

[英]How to parallelize a for loop in Python?

Some posts about parallelizing for loop in Python already exist such as this one but I can't use them to deal with my issue.一些关于在 Python 中并行化 for 循环的文章已经存在,例如这篇文章,但我不能用它们来处理我的问题。 Let's take a simple example.让我们举一个简单的例子。 I have three lists :我有三个列表:

L1 = [1,2,3]
L2 = [3,4,5]
L3 = [5,6,7]

I would like to double each element of the list.我想将列表中的每个元素加倍。 I could do this :我可以这样做:

for l in [L1,L2,L3] :
    for i in range(len(l)) :
        l[i] = l[i]*2

How please could I parallelize this code to transform L1, L2 and L3 in parallel ?请问如何并行化此代码以并行转换 L1、L2 和 L3?

Note that this example is just to have a clear and easy to understand example, I know that it is not a good idea in reality to parallelize a quick code like that请注意,这个例子只是为了有一个清晰易懂的例子,我知道在现实中并行化这样的快速代码并不是一个好主意

Using Asyncio:使用异步:

import asyncio
import time
def background(f):
    def wrapped(*args, **kwargs):
        return asyncio.get_event_loop().run_in_executor(None, f, *args, **kwargs)

    return wrapped

@background
def your_function(argument):
    time.sleep(2)
    print('function finished for '+str(argument))


for i in range(10):
    your_function(i)


print('loop finished')

Using multiprocessing:使用多处理:

from multiprocessing import Pool

L1 = [1,2,3]
L2 = [3,4,5]
L3 = [5,6,7]

def f(li):
    return [x * 2 for x in li]

if __name__ == '__main__':
    with Pool(4) as pool:
        print(pool.map(f, [L1, L2, L3]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM