python - Running multiple asynchronous function and get the returned value of each function -


i trying create function can run multiple processes asynchronous , send response. since multiprocessing.process() not return response, thought of creating function as:

from multiprocessing import process  def async_call(func_list):     """     runs list of function asynchronously.      :param func_list: expects list of lists of format         [[func1, args1, kwargs1], [func2, args2, kwargs2], ...]     :return: list of output of functions         [output1, output2, ...]     """     response_list = []     def worker(function, f_args, f_kwargs, response_list):         """         runs function , appends output list         """         response = function(*f_args, **f_kwargs)         response_list.append(response)      processes = [process(target=worker, args=(func, args, kwargs, response_list)) \                     func, args, kwargs in func_list]      process in processes:         process.start()     process in processes:         process.join()     return response_list 

within function, call worker asynchronously accepts additional parameter list. since, lists passed reference, thought can append response of actual function within list. , async_call return me response of function.

but not behaving way expected. value gets appended list within worker(), outside worker response_list list remains empty.

any idea doing wrong? and, there alternative achieve doing?

you can't share objects directly across processes. need use 1 of classes designed communicating values, queue , pipe; see the documentation.


Comments