如何将多个参数传递给一个函数,该函数由conCurent.futures.ProcessPoolExecutor中的ecutor.map()迭代 [英] How to pass several parameters to a function which is iterated by executor.map() from concurrent.futures.ProcessPoolExecutor

查看:0
本文介绍了如何将多个参数传递给一个函数,该函数由conCurent.futures.ProcessPoolExecutor中的ecutor.map()迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题在Stackoverflow中发布了几次,但没有一个对我有帮助,因为我的案例非常具体。

我有一个函数MyFunction(p1,p2,p3,p4),它需要4个参数才能运行。

我需要以多处理方式运行MyFunction。

我使用concurrent.futures.ProcessPoolExecutor来完成此工作。

我知道如何在Executor.map(MyFunction,Argument)中将列表作为参数传递

但这一次,我有一个元组列表,这是我的多处理的4个参数的列表。

以下是我的代码:

def Myfunction(p_udid,p_systemPort,p_deviceName, p_version, p_os):
    desired_caps = {}
    desired_caps['platformName'] = p_os
    desired_caps['platformVersion'] = p_version
    desired_caps['deviceName'] = p_deviceName
    desired_caps['udid'] = p_udid
    desired_caps['noReset'] = 'true'



if __name__ == '__main__':
    list=[('41492968379078','4730','S6S5IN3G','6','Android'),('53519716736397','4731','S6S5IN3G','6','Android'),('0123456789ABCDEF','4732','20','7','Android')]
    with concurrent.futures.ProcessPoolExecutor() as executor:
        multiprocesses = executor.map(Myfunction, list)

我当然会收到错误:

concurrent.futures.process._RemoteTraceback:"回溯(大多数 最近一次呼叫):文件 "C:UsersNinoAppDataLocalProgramsPythonPython37libconcurrentfuturesprocess.py", 第239行,进程内工作进程 R=Call_Item.fn(*Call_Item.args,**Call_Item.kwargs)文件"C:UsersNinoAppDataLocalProgramsPythonPython37libconcurrentfuturesprocess.py", 第198行,进程内区块 返回[FN(*args)for Args in Chunk]文件"C:UsersNinoAppDataLocalProgramsPythonPython37libconcurrentfuturesprocess.py", 第198行,输入 返回[fn(*args)for args in chunk]TypeError:MyFunction()缺少4个必需的位置参数:‘P_systemPort’, ‘p_deviceName’、‘p_version’和‘p_os’"

上述异常是以下异常的直接原因:

回溯(最近一次调用):文件 "E:/DropboxBACKUP14112018/Cff/Python/project_GITHUB/test2.py",线路 24,英寸 对于多进程中的多进程:文件"C:UsersNinoAppDataLocalProgramsPythonPython37libconcurrentfuturesprocess.py", 第483行,In_Chain_From_Iterable_Of_List 对于可迭代中的元素:文件"C:UsersNinoAppDataLocalProgramsPythonPython37libconcurrentfutures_base.py", 结果迭代器中的第598行 Year fs.op()."C:UsersNinoAppDataLocalProgramsPythonPython37libconcurrentfutures_base.py",()文件 第435行,结果 返回SELF。__GET_RESULT()文件"C:UsersNinoAppDataLocalProgramsPythonPython37libconcurrentfutures_base.py", 第384行,位于__GET_RESULT 引发SEEL._EXCEPTION类型错误:MyFunction()缺少4个必需的位置参数:‘p_systemPort’、‘p_deviceName’、‘p_version’和 ‘p_os

我尝试了与我的问题类似的所有答案中的不同内容,但没有成功。

有人能帮帮我吗?

推荐答案

以下是使其工作的方法:

def Myfunction(*args):

    p_udid,p_systemPort,p_deviceName, p_version, p_os = args[0]

    desired_caps = {}
    desired_caps['platformName'] = p_os
    desired_caps['platformVersion'] = p_version
    desired_caps['deviceName'] = p_deviceName
    desired_caps['udid'] = p_udid
    desired_caps['noReset'] = 'true'
    return desired_caps

def cpu_tasks(func, *args):

    # set chunksize to be even 
    with ProcessPoolExecutor() as tp:
        result = tp.map(func, chunksize=10, *args)
    return list(result)

if __name__ == '__main__':
    lst=[('41492968379078','4730','S6S5IN3G','6','Android'),('53519716736397','4731','S6S5IN3G','6','Android'),('0123456789ABCDEF','4732','20','7','Android')]
    ans = cpu_tasks(Myfunction, *[lst])

    print(ans)

[{'platformName': 'Android',
  'platformVersion': '6',
  'deviceName': 'S6S5IN3G',
  'udid': '41492968379078',
  'noReset': 'true'},
 {'platformName': 'Android',
  'platformVersion': '6',
  'deviceName': 'S6S5IN3G',
  'udid': '53519716736397',
  'noReset': 'true'},
 {'platformName': 'Android',
  'platformVersion': '7',
  'deviceName': '20',
  'udid': '0123456789ABCDEF',
  'noReset': 'true'}]

这篇关于如何将多个参数传递给一个函数,该函数由conCurent.futures.ProcessPoolExecutor中的ecutor.map()迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆