Easy methods to Parallelize a for Loop in Python

If that you must run a for loop in parallel, then you are able to do one of many following:

Choice 1 – Utilizing multiprocessing#

import multiprocessing

def sumall(worth):
    return sum(vary(1, worth + 1))

pool_obj = multiprocessing.Pool()

reply = pool_obj.map(sumall,vary(0,5))

Choice 2 – Utilizing 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 vary(5) for j in vary(2))

Choice 3 – Utilizing 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('operate completed for '+str(argument))

for i in vary(10):

print('loop completed')