Linux

Python multiprocessing

Python Multiprocessing is similar to multithreading, instead of creating threads multiprocessing create sub-processes. By using subprocess multiprocessing evade the GIL. ( Global Interpreter Lock) It runs on both Unix and Windows.

The Process class

The processes are spawned by creating a object of Process class then calling its start() method. Let's understand the working of process class by examples.
Python multiprocessing
Python process
Click to view code

In above example, we have just created one process. The multiprocessing syntax is very much similar to the multithreading.
Let us create Multiple processes.
Python multiprocessing
Python multiple process
Click to view code

In above example 5 process has been created.
Let us give each process a name and get its process id. In order to get name and process id, we are going to use current_process() class.
Let us see the example.
Python multiprocessing name and pid
Python multiple process
Click to view code

By using current_process's object we are printing process id and process name. Let us print the message after completion of tasks of subprocesses.
Let us see the example.
Python multiprocessing join
Python multiple process

After running two time the print statement "All finished" printed the message before subprocess finished the task. So how can main process print the message after subprocess finish the task. In order to do this, the main process must have waited until the subprocess finished the task. In order to do that we will use join() method
Python multiprocessing join()
Python multiple process join()
Click to view code

The join() method blocks the main process. If we don't give argument to join() method then join() waits indefinitely time. let us consider a another example of join with argument.

Python multiprocessing join()
Python multiple process join
Click to view code

In above example we have given arguement 2 in the join method. It means join() method will block the main process for 2 second. You can see that process p1 which target the function worker1 has to wait for 4 seconds and join of process p1 can block main process for 2 seconds. Let us understand more with is_alive() method.
Python multiprocessing join() is_alive()
Python multiple process join
Click to view code
Example is self explanatory. The is_alive() method checks weather a process is live or not.
In order to kill the process the terminate() method has been used. Let us see the example
Python multiprocessing join() is_alive() terminate()
Python multiple process terminate
Click to view code
You must be confused, how can a process be alive after termination. See the next example.
Python multiprocessing join() is_alive() terminate()
Python multiple process terminate
Click to view code
After termination of the process, 1 second has been given the background machinery to update the status of the object to reflect the termination. or you can use join() instead of time.sleep().

Daemon Process


So far we have created the non-daemon process. What is the daemon process ?
When a main process exits, it attempts to terminate all of its daemonic child processes.
Consider an example of GUI as shown below
Python multiprocessing daemon process
Python multiple daemon process
Consider, by GUI input some calculation is being performed in the background. And calculation is taking its time.
If you click close button two courses of action can be performed.
1. After clicking the close button the whole GUI window exists.
2. After clicking the close button the GUI window will wait for the completion of background calculation.

If first course of action is performed then Daemon process are being used in background calculation. If second course of action is performed then Non-daemon process are being used in background calculation.
In order to create daemon process See the example below.
Python multiprocessing daemon process
Python multiple daemon process
Click to view code
You can see the syntax of declaring daemon process. In above example all print statements are executed. The main process had to wait for non-daemon process.
Let us create an example where daemon process take 4 seconds.
Python multiprocessing daemon process
Python multiple daemon process
Click to view code
You can see the main process does not wait for daemon process to complete.
In next chapter, you will see the communication between the processes.






admin