Type to search…
Skip to content

Multiprocessament

A Python program uses a single CPU — `multiprocessing` distributes the work.

Updated

Taught in
Desenvolupament web en entorn servidorServidor webDAW-BIOProgramació d'intel·ligència artificialConcurrència i consum de serveis remotsIABD

Parallel programming

A Python program only uses 1 CPU of the processor even if the processor has several CPUs available.

With the multiprocessing module you can run a part of your program in a new process that will run on another CPU of the processor.

You do this when you have a computationally intensive function, which means it has to do a lot of things and takes a long time to do them, like calculating the Factorial of a fairly large number.

Below you have a factorial function:

python
def factorial(n):

    start_time = time.perf_counter()
    fact = 1
    for i in range(1, n + 1):
        fact = fact * i

    print(f"n = {n}: {time.perf_counter() - start_time : .4f} seconds")

If you run the factorial() function twice, you can see that the program takes more than 1 second to run because until the first call to the factorial() function has finished executing, the second call to the factorial() function cannot be executed.

python
import time

def factorial(n):

    start_time = time.perf_counter()
    fact = 1
    for i in range(1, n + 1):
        fact = fact * i

    print(f"n = {n}: {time.perf_counter() - start_time : .4f} seconds")


if __name__ == "__main__":

    factorial(100000)
    factorial(90000)

    print("Program finished")

If you run the program you can see that it takes about 5 seconds to run because it only uses one process and one CPU:

shell
time python3 multi.py 
n = 100000:  3.1057 seconds
n = 90000:  2.1048 seconds
Program finished

real    0m5,245s
user    0m4,799s
sys     0m0,446s

With the multiprocessing library you can create a process to run the factorial task.

python
...
import multiprocessing as mp

process = mp.Process(target=factorial, args= (100000,))

Keep reading — it's free.

The rest of this page is open to anyone with a free account. Nothing is sold here and nothing is charged for: the account exists so we know who agreed to the terms, and so we can send you the newsletter if you want it.

Create a free account

You will be asked to accept the Terms · Privacy Policy