运行多个异步函数并获取每个函数的返回值 [英] Running multiple asynchronous function and get the returned value of each function
问题描述
我试图创建一个可以异步运行多个进程并发送响应的函数.由于multiprocessing.Process()
不返回响应,因此我考虑将函数创建为:
I was trying to create a function which can run multiple processes asynchronous and will send the response. Since multiprocessing.Process()
do not return the response, I thought of creating a function as:
from multiprocessing import Process
def async_call(func_list):
"""
Runs the list of function asynchronously.
:param func_list: Expects list of lists to be of format
[[func1, args1, kwargs1], [func2, args2, kwargs2], ...]
:return: List of output of the functions
[output1, output2, ...]
"""
response_list = []
def worker(function, f_args, f_kwargs, response_list):
"""
Runs the function and appends the output to list
"""
response = function(*f_args, **f_kwargs)
response_list.append(response)
processes = [Process(target=worker, args=(func, args, kwargs, response_list)) \
for func, args, kwargs in func_list]
for process in processes:
process.start()
for process in processes:
process.join()
return response_list
在此函数中,我异步调用worker
,它接受其他参数作为list
.由于列表是作为参考传递的,所以我认为我可以在列表中附加实际功能的响应.然后async_call
将返回我所有功能的响应.
Within this function, I call worker
asynchronously which accepts additional parameter as list
. Since, lists are passed as reference, I thought I can append the response of actual function within the list. And async_call
will return me the response of all the function.
但是,这并不符合我的预期.值被附加到worker()
内的list
上,但在工作程序response_list
列表之外仍为空.
But this is not behaving the way I expected. Value gets appended to the list
within the worker()
, but outside the worker response_list
list remains empty.
知道我在做什么错吗?而且,有没有其他选择可以实现我正在做的事情?
Any idea what I am doing wrong? And, is there any alternative to achieve what I am doing?
推荐答案
您不能直接在进程之间共享对象.您需要使用为传递值而专门设计的类之一,即Queue和Pipe;参见文档.
You can't share objects directly across processes. You need to use one of the classes especially designed for communicating values, Queue and Pipe; see the documentation.
这篇关于运行多个异步函数并获取每个函数的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!