繁体   English   中英

如何将多个参数传递给由 concurrent.futures.ProcessPoolExecutor 中的 executor.map() 迭代的函数

[英]How to pass several parameters to a function which is iterated by executor.map() from concurrent.futures.ProcessPoolExecutor

该问题在 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:\\Users\\Nino\\AppData\\Local\\Programs\\Python\\Python37\\lib\\concurrent\\futures\\process.py", line 239、在_process_worker r = call_item.fn(*call_item.args, **call_item.kwargs) 文件“C:\\Users\\Nino\\AppData\\Local\\Programs\\Python\\Python37\\lib\\concurrent\\futures\\process.py” ,第 198 行,在 _process_chunk 中返回 [fn(*args) for args in chunk] 文件“C:\\Users\\Nino\\AppData\\Local\\Programs\\Python\\Python37\\lib\\concurrent\\futures\\process.py”,第 198 行, 作为回报 [fn(*args) for args in chunk] TypeError: Myfunction() missing 4 required positional arguments: 'p_systemPort', 'p_deviceName', 'p_version', and 'p_os' """

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

回溯(最近一次调用):文件“E:/DropboxBACKUP14112018/Cff/Python/project_GITHUB/test2.py”,第 24 行,用于多进程中的多进程:文件“C:\\Users\\Nino\\AppData\\Local\\Programs\\ Python\\Python37\\lib\\concurrent\\futures\\process.py", line 483, in _chain_from_iterable_of_lists for element in iterable: File "C:\\Users\\Nino\\AppData\\Local\\Programs\\Python\\Python37\\lib\\concurrent\\futures_base. py", line 598, in result_iterator yield fs.pop().result() File "C:\\Users\\Nino\\AppData\\Local\\Programs\\Python\\Python37\\lib\\concurrent\\futures_base.py", line 435, in结果返回 self.__get_result() 文件“C:\\Users\\Nino\\AppData\\Local\\Programs\\Python\\Python37\\lib\\concurrent\\futures_base.py”,第 384 行,在 __get_result 中引发 self._exception TypeError: Myfunction() missing 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'}]

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM