In Python’s multiprocessing
module, the start()
and join()
methods are used to control the execution of a Process
object.
start()
The start()
method is used to start a new process, which begins running the target function specified during the Process
object’s creation. When you call start()
on a Process
object, the following happens:
- A new process is created by the operating system.
- The new process begins executing the target function, along with any arguments passed to it, in a separate environment with its own memory space.
from multiprocessing import Process |
join()
The join()
method is used to block the main process (i.e., the script that initiated the new process) until the target process completes its execution. This is useful for synchronizing processes and ensuring that the main process waits for all child processes to finish before continuing.
When you call join()
on a Process
object, the following happens:
- The main process is blocked and waits for the target process to finish its execution.
- Once the target process is completed, the
join()
method returns, and the main process continues executing.
from multiprocessing import Process |
In this example, the join()
method ensures that the “Process completed” message is printed only after the my_function
has finished executing in the new process.