[英]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.