This article — a short but short introduction to multiprocessing in the Python programming language.
What is multiprocessing?
Under multiprocessing refers to the ability of a system to support more than one processor at a time. Applications on a multiprocessor system are broken down into smaller routines that run independently. The operating system distributes these threads among the processors, improving system performance.
Consider a computer system with one processor. If it has multiple processes assigned at the same time, it will have to interrupt each task and briefly switch to another for all processes to work.
This situation is similar to how a cook works alone in a kitchen. It has to do several tasks like baking, stirring, kneading dough, etc.
So the bottom line is: the more tasks you have to complete at the same time, the more difficult it becomes to keep track of them all, and keeping to the right time is becoming more and more challenging.
This is where the concept of multiprocessing comes in!
A multiprocessor system can have:
Here a processor can easily execute multiple tasks at the same time, with each task using its own processor.
It`s like a chef in the last situation, assisted by his assistants. Now they can share tasks among themselves, and the chef does not need to switch between his tasks.
Multiprocessing in Python
In Python module multiprocessing includes a very simple and intuitive API for division of work among several processes.
Let`s take a look at a simple example of using a multiprocessing module:
Square: 100 Cube: 1000 Done!
Let`s try to understand the above code:
Note. The process constructor also accepts many other arguments, which will be discussed later. In the above example, we have created 2 processes with different target functions:
p1 = multiprocessing.Process (target = print_square, args = (10,)) p2 = multiprocessing.Process (target = print_cube, args = ( 10,))
p1.start () p2. start ()
p1.join () p2.join ()
As a result, the current program will first wait for completion p1, and then p2 . Once they are complete, the following statements of the current program are executed.
Let`s take a look at another program to understand the concept of different processes running on the same Python script. In this example below, we print the identifier of the processes that perform the target functions:
ID of main process: 28628 ID of process running worker1: 29305 ID of process running worker2: 29306 ID of process p1: 29305 ID of process p2: 29306 Both processes finished execution! Process p1 is alive: False Process p2 is alive: False
Note that it matches the process IDs of p1 and p2, which we get using the pid attribute of the Process class.
Consider the diagram below to understand how the new processes differ from the main Python script:
So this was a short introduction to multiprocessing in Python ... The next few articles will focus on the following topics related to multiprocessing:
This article courtesy of Nikhil Kumar . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.
Please post comments if you find anything wrong or if you`d like to share more information on the topic discussed above.