Issue
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.
Example 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:
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))
Solution
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. For my function, it took in multiple arguments, so I organized my arguments into a list of tuples.
Answered By - Anders Zhou Answer Checked By - Marie Seifert (Python.Easybugfix Admin)
Post a Comment
Post a Comment