I have a problem with multiprocessing to use more than 1 core. I didn't use .join() which waits the previous process to finish, i have a CPU with 4 cores and i still don't know why this code only uses %20 of the CPU, it supposed to use more than %50.
import multiprocessing
import time
def f(x,y):
global StartTime
for ord1 in range(x):
x *= 2
print(y, time.time()-StartTime)
if __name__ == "__main__":
input1 = 500000
StartTime = time.time()
p1 = multiprocessing.Process(target=f(input1,1))
p2 = multiprocessing.Process(target=f(input1,2))
p3 = multiprocessing.Process(target=f(input1,3))
p4 = multiprocessing.Process(target=f(input1,4))
p1.start()
p2.start()
p3.start()
p4.start()
print("Done")
I guess the program executes them one by one instead of parallel judging from the output:
1 5.373946189880371
2 10.663974285125732
3 15.902992725372314
4 21.29733967781067
Done
So, my question is how do i use multiple cores and execute the code parallelly?