Skip to content

Instantly share code, notes, and snippets.

@naranjja
Created March 15, 2019 05:11
Show Gist options
  • Save naranjja/72373ecdc4ba911d39c4dbd9b95b2d82 to your computer and use it in GitHub Desktop.
Save naranjja/72373ecdc4ba911d39c4dbd9b95b2d82 to your computer and use it in GitHub Desktop.
Simple parallelizing of for loop in Python
import multiprocessing, joblib
def parallelize(iter_expression, func):
func = func.replace("(", ")(")
n_jobs = multiprocessing.cpu_count()
return eval("joblib.Parallel(n_jobs={})(joblib.delayed({} {})".format(n_jobs, func, iter_expression))
parallelize("for i in range(10)", "print(i)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment