In normal Python code, I can understand the lifecycle of the process. e.g. when executing python script.py
:
- shell receives the command
python script.py
, and os creates a new process to start executingpython
. python
executable sets up the interpreter, and starts to executescript.py
.- When
script.py
finishes execution, python interpreter will exit.
In the case of multiprocessing, I’m curious about what happens to the other processes.
Take the following code for example:
# test.py
import multiprocessing
# Function to compute square of a number
def compute_square(number):
print(f'The square of {number} is {number * number}')
if __name__ == '__main__':
# List of numbers
numbers = [1, 2, 3, 4, 5]
# Create a list to keep all processes
processes = []
# Create a process for each number to compute its square
for number in numbers:
process = multiprocessing.Process(target=compute_square, args=(number,))
processes.append(process)
process.start()
# Ensure all processes have finished execution
for process in processes:
process.join()
print("All processes have finished execution.")
When I execute python test.py
, I understand that test.py
will be executed as __main__
module. But what happens to the other processes?
To be specific, when I execute multiprocessing.Process(target=compute_square, args=(number,)).start()
, what happens to that process?
How does that process invoke the python interpreter? if it is just python script.py
, how does it know it needs to execute a function named compute_square
? or does it uses python -i
, and pass the command to execute through a pipe?