[英]Multiprocessing either never finishes or takes longer than serial processing
For some reason, Example 1's multiprocessing portion prints out a time far greater than the serial portion, and Example 2 just runs forever... I'm wondering if this is an issue with my using VSCode/Jupyter Notebook rather than the code itself, which I got from a couple tutorials I found online.出于某种原因,示例 1 的多处理部分打印出的时间远大于串行部分,而示例 2 只是永远运行......我想知道这是否是我使用 VSCode/Jupyter Notebook 而不是代码本身的问题,我从网上找到的几个教程中得到的。
Example 1:示例 1:
import multiprocessing as mp
import time
import math
results_a = []
results_b = []
def calc_one(numbers):
for number in numbers:
results_a.append(math.sqrt(number **4))
def calc_two(numbers):
for number in numbers:
results_b.append(math.sqrt(number**5))
if __name__ == '__main__':
number_list = list(range(20))
start = time.time()
calc_one(number_list)
calc_two(number_list)
end = time.time()
print("Time for process one: " + str(end - start))
start = time.time()
p1 = mp.Process(target = calc_one, args = (number_list,))
p2 = mp.Process(target = calc_two, args = (number_list,))
p1.start()
p2.start()
end = time.time()
print("Time for process two: " + str(end - start))
Example 2:示例 2:
def f(n):
sum = 0
for x in range(100):
sum += x*x
return sum
if __name__ == "__main__":
array = [1, 2, 3, 4, 5]
start = time.time()
result = []
for n in array:
result.append(f(n))
print(result)
end = time.time()
print("Time for process one: " + str(end - start))
start = time.time()
result = []
p = Pool()
result = p.map(f, array)
p.close()
p.join()
print(result)
end = time.time()
print("Time for process one: " + str(end - start))
The solution that ended up working for me was:最终为我工作的解决方案是:
func(num):
print(num * num)
import multiprocessing.dummy as mp
p = mp.Pool(5)
list = p.starmap(func, y for y in range(3, 100))
p.close()
p.join()
The process I ran contained a function that was quite a bit more intensive than the one shown above as func(), but it still worked.我运行的进程包含一个 function,它比上面显示的 func() 更加密集,但它仍然有效。 For my function, it took in multiple arguments, so I organized my arguments into a list of tuples.对于我的 function,它包含多个 arguments,所以我将 arguments 组织到一个元组列表中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.