Python:获取多个返回值并在parallel.futures.ProcessPoolExecutor()的executor.map中提供多个参数

时间:2019-02-12 18:30:36

标签: python-3.x python-multiprocessing threadpoolexecutor

我对Python多处理概念非常陌生。 我正在尝试调用函数magicFunction,该函数具有多个参数,其中第一个参数是可迭代的,而所有其他参数都是不可迭代的。另外,它返回多个值,我们说x, y, z

我正在尝试弄清楚如何使用executor。这是我的方法,这显然是错误的。

def magicFunction(webElem, uid_list, ignoreTagsList):
    ..
    ..
    return x,y,z

with concurrent.futures.ProcessPoolExecutor() as executor:
    for webElem, x_val, y_val, z_val in zip(webElem_list, executor.map(magicFunction, webElem_list, uid_list, ignoreTagsList)):
    ..
    ..
    print("Values:", x_val, y_val, z_val)

有人可以建议正确的方法吗?

2 个答案:

答案 0 :(得分:0)

您可以使用一个类:

class FunctionReturn:
    x = 0
    y = 0
    z = 0

def myFunction():
    output = FunctionReturn()
    output.x = 1
    output.y = 2
    output.z = 3
    return output

data = myFunction()
print(data.x , data.y , data.z)

这将打印1 2 3

答案 1 :(得分:0)

import concurrent.futures

## This works list of numbers
def square(number):
    return number ** 2, True

number = [1,2,3,4,5]
with concurrent.futures.ThreadPoolExecutor() as executor:
    future = [executor.submit(square, i) for i in number]
    print (future)
    return_value = [i.result() for i in future]
    print (return_value)
    d = {k:v for k, v in return_value} ##if you want in dictionary
    for k, v in return_value:  ##if want iterate and get the values
        print (k, v)




import concurrent.futures

def square(number):
    return number ** 2, True

with concurrent.futures.ThreadPoolExecutor() as executor:
    future = executor.submit(square, 5)
    return_value, bool_value  = future.result()
    print(return_value, bool_value)

o/p: 25 True