I would like to define a do_in_parallel
function in python that will take in functions with arguments, make a thread for each and perform them in parallel. The function should work as so:
do_in_parallel(_sleep(3), _sleep(8), _sleep(3))
I am however having a hard time defining the do_in_parallel
function to take multiple functions with multiple arguments each, here's my attempt:
from time import sleep
import threading
def do_in_parallel(*kwargs):
tasks = []
for func in kwargs.keys():
t = threading.Thread(target=func, args=(arg for arg in kwargs[func]))
t.start()
tasks.append(t)
for task in tasks:
task.join()
def _sleep(n):
sleep(n)
print('slept', n)
Using it as so, and getting the following error:
do_in_parallel(_sleep=3, _sleep=8, _sleep=3)
>> do_in_parallel(sleepX=3, sleepX=8, sleepX=3)
^
>> SyntaxError: keyword argument repeated
Can someone explain what I would need to change in my function so that it can take multiple function parameters as so:
do_in_parallel(_sleep(3), _sleep(8), maybe_do_something(else, and_else))