![]() |
Python’s multiprocessing library provides a powerful way to leverage multiple processor cores for concurrent execution, enhancing the performance of computationally intensive tasks. One of the intriguing aspects of multiprocessing is the ability to initiate new processes using various start methods. These start methods not only facilitate the creation of child processes but also offer control mechanisms for managing the flow of execution between parent and child processes. Start MethodsA start method is a technique to start the child process in Python, There are 3 start methods
You can identify which technique was used as the start method by the multiprocessing module in Python by executing the following command: Python3
Output
fork You can set the desired start method type by following the snippet: Python3
The type of start method used depends on the operating system, and some operating systems might not support certain start methods. The following lists major OS Platforms and default start methods
As of now we focus on fork and spawn method ForkThis fork method works on creating the exact clone or copy of the parent process. i.e resources utilised or altered by the parent process will be accessed by the child. For Example:If you declare a global variable num and set its value to 10, then modify the value of this variable within the parent process before creating a child process. The child process after being forked, will inherit the updated value of num from the parent. In simpler terms, the child process receives a copy of the existing process, including any changes made on the resources in the fork method. Explanation The script initializes “num” as 10 globally, accessible to both the main and child processes. Main process updates it to 20, but child process inherits the modified value 20. The child process prints 20, increments it to 21. In the script showcased, the ‘fork’ method is used to create child processes that inherit a snapshot of the parent process’s memory, including variable values. However, changes made to shared variables within child processes don’t impact the parent process. Python3
Output
fork In parent process before update 10 In parent process after update: 20 In child process before update: 20 In child process after update: 21 At the end the vaule is: 20 SpawnThis spawn method works on creating new process from the parent process. i.e resources utilised or altered by the parent process will not get reflected in child process as we seen in fork mechanism. ![]() spawn start method For Example:If you declare a global variable num and set its value to 10, then modify the value of this variable within the parent process before creating a child process, the child process after being spawned will get access the value num as 10 and not the parent changed value 20. Means the spawn will tend to create a new fresh process and will not inherit the changes from parent. Explanation The script initializes “num” as 10 globally, accessible to both the main and child processes. Main process updates it to 20, but child process inherits the original 10. The child process prints 10, increments it to 11. In the script showcased, the ‘spawn’ method is employed to generate child processes, each equipped with an entirely fresh memory space. This method ensures that changes made to shared variables within child processes remain contained and do not affect the parent process. Python3
Output
spawn In parent process before update 10 In parent process after update: 20 In child process before update: 10 In child process after update: 11 At the end the vaule is: 20 Difference between Fork and Spawn methods.
Summary
|
Reffered: https://www.geeksforgeeks.org
Operating Systems |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 19 |