[英]How to pass several parameters to a function which is iterated by executor.map() from concurrent.futures.ProcessPoolExecutor
The issue was posted several times here in stackoverflow, but none of them could help me as my case is very specific.该问题在 stackoverflow 中多次发布,但没有一个可以帮助我,因为我的情况非常具体。
I have a function Myfunction(p1,p2,p3,p4) which need 4 parameters to run.我有一个函数 Myfunction(p1,p2,p3,p4) 需要 4 个参数才能运行。
I need to run Myfunction on multiprocessing way.我需要以多处理方式运行 Myfunction。
I use concurrent.futures.ProcessPoolExecutor to do this job.我使用concurrent.futures.ProcessPoolExecutor来完成这项工作。
I know how to pass a list as an argument in the executor.map(Myfunction, argument)我知道如何在 executor.map(Myfunction, argument) 中将列表作为参数传递
But this time, I have a list of tuples which is the list of my 4 arguments for my multiprocessing.但这一次,我有一个元组列表,它是我的多处理的 4 个参数的列表。
Here is my code:这是我的代码:
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)
Of course I get an error :当然我得到一个错误:
concurrent.futures.process._RemoteTraceback: """ Traceback (most recent call last): File "C:\\Users\\Nino\\AppData\\Local\\Programs\\Python\\Python37\\lib\\concurrent\\futures\\process.py", line 239, in _process_worker r = call_item.fn(*call_item.args, **call_item.kwargs) File "C:\\Users\\Nino\\AppData\\Local\\Programs\\Python\\Python37\\lib\\concurrent\\futures\\process.py", line 198, in _process_chunk return [fn(*args) for args in chunk] File "C:\\Users\\Nino\\AppData\\Local\\Programs\\Python\\Python37\\lib\\concurrent\\futures\\process.py", line 198, in return [fn(*args) for args in chunk] TypeError: Myfunction() missing 4 required positional arguments: 'p_systemPort', 'p_deviceName', 'p_version', and 'p_os' """
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' """
The above exception was the direct cause of the following exception:
上述异常是以下异常的直接原因:
Traceback (most recent call last): File "E:/DropboxBACKUP14112018/Cff/Python/project_GITHUB/test2.py", line 24, in for multiprocess in multiprocesses: File "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 result return self.__get_result() File "C:\\Users\\Nino\\AppData\\Local\\Programs\\Python\\Python37\\lib\\concurrent\\futures_base.py", line 384, in __get_result raise self._exception 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”
I tried different stuff from all the answers similar to my issue, but I didn't succeed.我尝试了与我的问题类似的所有答案中的不同内容,但我没有成功。
Can someone help me please?有人能帮助我吗?
Here's a way you can make it work:这是您可以使其工作的一种方法:
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.