Skip to content

Instantly share code, notes, and snippets.

@aladinoster
Created October 28, 2021 13:13
Show Gist options
  • Save aladinoster/7b7d3d3fef2079c4eff4448348cd1203 to your computer and use it in GitHub Desktop.
Save aladinoster/7b7d3d3fef2079c4eff4448348cd1203 to your computer and use it in GitHub Desktop.
Python: Launch in paralell diferent functions
from multiprocessing import Process
from tqdm import tqdm
from functools import partial
import time
def runInParallel(*fns):
"""
Handle processs in paralell
"""
proc = []
N = -1
for fn in fns:
p = Process(target=fn)
p.start()
print(f"Start with args {fn.args}")
proc.append(p)
N += 1
for p, _ in zip(proc, tqdm(range(N))):
p.join()
def my_simulation(x, a):
"""One simulation"""
time.sleep(x ** a)
print(f"I slept for {x**a} seconds")
def process_case(cases=[2, 3, 4]):
fns = []
for case in cases:
fns.append(partial(my_simulation, 2, case))
runInParallel(*fns)
if __name__ == "__main__":
process_case([1, 2, 3])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment