1

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?

1 Answer 1

2

When starting a process, you need to provide the arguments to your target function via args. See https://docs.python.org/3/library/multiprocessing.html#the-process-class. Otherwise, f(input1, 1) is calculated in your main process, not the worker.

Your main routine should be:

if __name__ == "__main__":
    input1 = 500000
    StartTime = time.time()
    p1 = multiprocessing.Process(target=f, args=(input1,1))
    p2 = multiprocessing.Process(target=f, args=(input1,2))
    p3 = multiprocessing.Process(target=f, args=(input1,3))
    p4 = multiprocessing.Process(target=f, args=(input1,4))
    p1.start()
    p2.start()
    p3.start()
    p4.start()
    print("Done")
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.