Multiprocessament
A Python program uses a single CPU — `multiprocessing` distributes the work.
Updated
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:
=
= 1
= *
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.
=
= 1
= *
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:
n = 100000: 3.1057 seconds
n = 90000: 2.1048 seconds
Program finished
real 0m5,245s
user 0m4,799s
sys 0m0,446sWith the multiprocessing library you can create a process to run the factorial task.
...
= 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 accountYou will be asked to accept the Terms · Privacy Policy