[英]How to use multiprocessing in Python in this case?
I try to use simple way to make mulitiprocessing: 我尝试使用简单的方法来进行多重处理:
urls = [1, 2, 3, 4]
p = Pool(2)
p.map(open, urls)
Where open()
is function, that does calculations. open()
是函数,它执行计算。
def open(url):
print(url)
When I do print(url)
it returns me strange result: 当我print(url)
它返回奇怪的结果:
1
2
3
4
1
2
3
4
I can assume that each of process Pool(2)
handler the same calculations there is wrong. 我可以假设每个进程Pool(2)
处理程序都有相同的计算错误。
I need that process 1 takes [1, 2]
for handling and process 2 takes [3, 4]
我需要处理1处理[1, 2]
处理和处理2需要[3, 4]
I think this should do what you want to: 我认为这应该做你想做的事:
import multiprocessing
def func(x):
return x[0] + x[1]
if __name__ == "__main__":
p = multiprocessing.Pool(2)
for x in p.map(func, [(1, 2), (3, 4)]):
print(x)
output: 输出:
3
7
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.