简体   繁体   English

将多个 arguments(数据除外)传递给 imap function 用于 Python 中的多处理

[英]Passing multiple arguments (other than data) to imap function for multiprocessing in Python

I am working on a simple multiprocessing program in python where I am passing a dataset where each pair of the list will return the multiplication of them.我正在 python 中开发一个简单的多处理程序,我正在传递一个数据集,其中每对列表将返回它们的乘积。 But when I am trying to pass a string along with the dataset to specify the operation (basically if I pass 'add' it will add the pair of numbers and if I pass 'sub' then it will subtract the pair of numbers) then I am facing all kinds of errors.但是,当我尝试将字符串与数据集一起传递以指定操作时(基本上,如果我传递'add',它将添加这对数字,如果我传递'sub',则它将减去这对数字)然后我我面临着各种各样的错误。 I tried to use the concept of partial function also but it was not that helpful.我也尝试使用部分 function 的概念,但这并没有太大帮助。 Here I am attaching my basic code if anybody can help me to modify the code then it will be a great help在这里我附上我的基本代码,如果有人可以帮助我修改代码,那将是一个很大的帮助

import multiprocessing as mp

data=([1,2],[2,3],[3,4],[4,5],[5,6],[1,2],[2,3],[3,4],[4,5],[5,6],[1,2],[2,3],[3,4],[4,5],[5,6],[1,2],[2,3],[3,4],[4,5],[5,6],
[1,2],[2,3],[3,4],[4,5],[5,6],[1,2],[2,3],[3,4],[4,5],[5,6],[1,2],[2,3],[3,4],[4,5],[5,6],[1,2],[2,3],[3,4],[4,5],[5,6]
,[1,2],[2,3],[3,4],[4,5],[5,6],[1,2],[2,3],[3,4],[4,5],[5,6],[1,2],[2,3],[3,4],[4,5],[5,6])

def mul(x,y):
   return(x*y)

def data_param(data):
    return mul(data[0],data[1])
def pool_handler():
    #p1=input('enter plus/mul: ')
    p=mp.Pool(3)
    
    out=p.imap(data_param,data)
    for i in out:
        print(f"output: {i}")
if __name__ == '__main__':
    pool_handler()

Thank you in advance先感谢您

While not a direct solution to using imap, for multiprocessing I find apply_async more flexible in general use.虽然不是使用 imap 的直接解决方案,但对于多处理,我发现 apply_async 在一般使用中更灵活。

For the job here:对于这里的工作:

def mul(x,y):
    return(x*y)

def div(x,y):
    return(x/y)

def data_param(data, op):
    if op == "mul":
        return mul(data[0],data[1])
    elif op == "div":
        return div(data[0],data[1])
    else:
        print("You'll not get this print return in windows warning you of an error so beware!")


# ------------------------------------------------------------------
if __name___ == "__main__":

    data = [[1,2],[2,3],[3,4],[4,5],[5,6]]  #Cut down to reduce clutter

    op = "mul"

    pool = mp.Pool(mp.cpu_count())
    jobs = []

    for dataBit in data:
        jobs.append(pool.apply_async(data_param, args=(dataBit, op)))

    results = [] 
    for jobBit in jobs:
        results = jobBit.get()

    pool.close()

Of course, you could probably directly use the operator passed into data_param with some clever footwork - but for the purposes of verbosity here, best left simple!当然,您可能可以通过一些巧妙的步法直接使用传递给 data_param 的运算符 - 但为了此处冗长的目的,最好保持简单!

A simple way is to pass the function along with its parameters:一种简单的方法是传递 function 及其参数:

...
def mul(x, y):
    return (x * y)

def add(x, y):
    return x + y

def data_param(data):
    return data[2](data[0], data[1])

def pool_handler():

    p1 = input('enter plus/mul: ')
    if p1 == 'plus':
        func = add
    elif p1 == 'mul':
        func = mul
    else:
        print('Incorrect value')
        exit(1)

    p = mp.Pool(3)

    out = p.imap(data_param, ((elt[0], elt[1], func) for elt in data))
    for i in out:
        print(f"output: {i}")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过 pool.imap() function 传递数据以外的参数以在 python 中进行多处理? - how to pass parameters other than data through pool.imap() function for multiprocessing in python? 多处理,在Python中的进程之间传递多个参数和数据同步 - Multiprocessing, Passing multiple arguments and Data synchronization between processes in Python Python多处理池传递函数参数 - Python Multiprocessing Pool passing function arguments 在Python 2.7中使用多个参数进行多处理以实现功能 - Multiprocessing with multiple arguments to function in Python 2.7 具有多个参数和 void 函数的 Python 多处理池 - Python Multiprocessing pool with multiple arguments and void function Python 多处理问题关于 function 多个 arguments 的迭代 - Python multiprocessing question on iteration of a function multiple arguments 带有多个参数的python多重处理 - python multiprocessing with multiple arguments Python - 将带有多个 arguments 的 function 传递给另一个 function - Python - Passing a function with multiple arguments into another function 使用python的多处理并行处理具有多个list参数的函数 - Parallelizing a function with multiple lists arguments with python's multiprocessing python多处理中将多个可迭代项作为参数 - Multiple iterables as arguments in python multiprocessing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM