Skip to content

Instantly share code, notes, and snippets.

@hmahadik
Created September 8, 2023 17:33
Show Gist options
  • Save hmahadik/2a5c93f0034452b0e6160d132711429a to your computer and use it in GitHub Desktop.
Save hmahadik/2a5c93f0034452b0e6160d132711429a to your computer and use it in GitHub Desktop.
Parallelize any function using multi-processing in python
""" Based on https://stackoverflow.com/questions/67132064/python-multiprocessing-pool-as-decorator """
import functools
import time
from multiprocessing import Pool
def parallelize(func=None, **options):
if func is None:
return functools.partial(parallel, **options)
def wrapper(iterable, *args, **kwargs):
processes = options["processes"]
with Pool(processes) as pool:
result = pool.map(func, iterable)
return result
return wrapper
def test(kwargs):
foo = kwargs['foo']
pid = kwargs['pid']
print(f"Process {pid}: foo = {foo}")
def main():
parallelize(test, processes=6)([dict(pid=i, foo="bar") for i in range(10)])
if __name__ == "__main__":
main()
@hmahadik
Copy link
Author

hmahadik commented Sep 8, 2023

Output should look like:

Process 0: foo = bar
Process 1: foo = bar
Process 2: foo = bar
Process 3: foo = bar
Process 4: foo = bar
Process 6: foo = bar
Process 7: foo = bar
Process 5: foo = bar
Process 9: foo = bar
Process 8: foo = bar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment