Skip to content

Multiprocessing

Farrokh Karimi edited this page Aug 6, 2018 · 8 revisions

In this section, we will focus more on the comparison between multiprocessing and multithreading.

Multiprocessing: It is the use of two or more CPUs units within a single computer system. It is the best approach to get the full potential from our hardware by utilizing full number of CPU cores available in our computer system.

Multithreading: It is the ability of a CPU to manage the use of operating system by executing multiple threads concurrently. The main idea of multithreading is to achieve parallelism by dividing a process into multiple threads.

The following table shows some of the important differences between them:

Multiprocessing Multiprogramming
1 Multiprocessing refers to processing of multiple processes at same time by multiple CPUs. Multiprogramming keeps several programs in main memory at the same time and execute them concurrently utilizing single CPU.
2 It utilizes multiple CPUs. It utilizes single CPU.
3 It permits parallel processing. Context switching takes place.
4 Less time taken to process the jobs. More Time taken to process the jobs.
5 It facilitates much efficient utilization of devices of the computer system. Less efficient than multiprocessing.
6 Usually more expensive. Such systems are less expensive.

Eliminating impact of GIL

While working with concurrent applications, there is a limitation present in Python called the GIL (Global Interpreter Lock). GIL never allows us to utilize multiple cores of CPU and hence we can say that there are no true threads in Python. GIL is the mutex – mutual exclusion lock, which makes things thread safe. In other words, we can say that GIL prevents multiple threads from executing Python code in parallel. The lock can be held by only one thread at a time and if we want to execute a thread then it must acquire the lock first. With the use of multiprocessing, we can effectively bypass the limitation caused by GIL:

  • By using multiprocessing, we are utilizing the capability of multiple processes and hence we are utilizing multiple instances of the GIL.
  • Due to this, there is no restriction of executing the bytecode of one thread within our programs at any one time.

Starting Processes in Python

The following three methods can be used to start a process in Python within the multiprocessing module:

  • Fork
  • Spawn
  • Forkserver

Creating a process with Fork

Fork command is a standard command found in UNIX. It is used to create new processes called child processes. This child process runs concurrently with the process called the parent process. These child processes are also identical to their parent processes and inherit all of the resources available to the parent. The following system calls are used while creating a process with Fork:

  • fork(): It is a system call generally implemented in kernel. It is used to create a copy of the process.
  • getpid(): This system call returns the process ID(PID) of the calling process.

Example: The following Python script example will help you understabd how to create a new child process and get the PIDs of child and parent processes:

Clone this wiki locally