Software Engineering

How to Parallelize a for Loop in Python

If you need to run a for loop in parallel, then you can do one of the following:

Option 1 – Using multiprocessing

import multiprocessing

def sumall(value):
    return sum(range(1, value + 1))

pool_obj = multiprocessing.Pool()

answer =,range(0,5))

Option 2 – Using joblib module

from joblib import Parallel, delayed
import math

def sqrt_func(i, j):
    return math.sqrt(i**j)

Parallel(n_jobs=2)(delayed(sqrt_func)(i, j) for i in range(5) for j in range(2))

Option 3 – 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

def your_function(argument):
    print('function finished for '+str(argument))

for i in range(10):

print('loop finished')