Sep-06-2018, 06:27 PM
Hello! I'm trying to fork out the calculation of factorial using multiprocessing. I'm new to this, but it seemed to me that using Pool could solve my problem. I want to use a certain number of workers that would parallelize the calculations. The problem is that I did not get any effect and multiprocessing works as if only one process is involved. What's wrong with my code?
from multiprocessing import Pool import time def factorial(number): total = 1 while number != 1: total = total * number number = number - 1 print(total) if __name__ == '__main__': time_start = time.time() workers_count = 4 pool = Pool(processes=workers_count) # start 4 worker processes res = pool.apply_async(factorial, (75000,)) # evaluate res.get() time_end = time.time() print("time with 4 multiprocessing: " + str(time_end - time_start)) time_start = time.time() factorial(75000) time_end = time.time() print("time as usual: " + str(time_end - time_start))