Parent Process vs Child Process in Python - Super Fast Python

Parent processes have one or more child processes, whereas child processes do not have any child processes.

In this tutorial you will discover the difference between the Parent and Child processes and when to use each in your Python projects.

Let’s get started.

What is a Parent Process

A parent process is a process that is capable of starting child processes.

Typically, we would not refer to a process as a parent process until it has created one or more child processes. This is the commonly accepted definition.

Recall, a process is an instance of a computer program. In Python, a process is an instance of the Python interpreter that executes Python code.

In Python, the first process created when we run our program is called the ‘MainProcess‘. It is also a parent process and may be called the main parent process.

You can learn more about the main process in the tutorial:

The main process will create the first child process or processes.

A child process may also become a parent process if it in turn creates and starts a child process.

A parent process typically will not terminate until all child processes created by the parent have terminated.

A process can be configured to be a daemon process when it is created. Daemon processes are used for background tasks and generally cannot create child processes.

You can learn more about daemon processes in the tutorial:

Now that we are familiar with parent processes, let’s take a look at child processes.

What is a Child Process

A child process is a process that was created by another process.

A child process may also be called a subprocess, as it was created by another process rather than by the operating system directly. That being said, the creation and management of all processes is handled by the underlying operating system.

The process that creates a child process is called a parent process. A child process only ever has one parent process.

There are three main techniques used to create a child process, referred to as process start methods.

They are:

  • Fork: Create a copy or fork of a parent process to run the specified code.
  • Spawn: Create a new instance of the Python interpreter to run the specified code.
  • Fork Server: Create a copy or fork of a parent process and use it as a template for all future child processes.

You can learn more about process start methods in the tutorial:

Depending on the technique used to start the child, the child process may or may not inherit properties of the parent process. For example, a forked process may inherit a copy of the global variables from the parent process.

A child process may become orphaned if the parent process that created it is terminated.

You can learn more about orphan child processes in the tutorial:

Now that we are familiar with the parent process and the child process, let’s compare and contrast each.

Comparison of Parent vs Child

Now that we are familiar with the parent and child processes, let’s review their similarities and differences.

Similarities Between Parent and Child Processes

The Parent and Child processes are very similar, let’s review some of the most important similarities.

  1. They are both Python processes.
  2. They both execute code in new native processes.
  3. A child process may be a parent, and a parent may be a child process.

Both parent and child processes are Python processes.

This means that they are instances of the Python interpreter and execute Python code.

Both parent and child processes are also instances of native processes.

This means that the management of when parent and child processes run is controlled by the underlying operating system. As such, they are also both assigned process identifiers (PIDs) by the operating system.

Both parent and child processes may be instances of each other.

A parent process may also be a child process of another Python process. A child process may also be a parent to another Python process.

Differences Between Parent and Child Processes

The Parent and Child processes are also quite different, let’s review some of the most important differences.

  1. A child is a leaf in the process tree, a parent is a node.
  2. The main process is not a child process.
  3. Daemon child processes cannot create children processes.

If we create many processes in a program, we might consider the relationship between the processes to form a tree structure.

The main process would be the root of the tree. Parent processes would be nodes in the tree. Child processes would be the leaves of the tree.

In this way, parent and child processes have a different relationship. Parent processes have child processes, child processes do not.

For example, a parent process may access its own parent process (if not the main process) via the multiprocessing.parent_process() function, and access the child processes via the multiprocessing.active_children() function. Child process can access the parent process but cannot access any child processes as they do not have any children by definition.

The main process is not a child process, although it can be a parent process if it creates child processes.

This is because the main process is created directly by the operating system.

In a related way, daemon processes are only child processes.

This is because although a parent process may create daemon processes, daemon processes themselves are unable to create child processes.


Free Python Multiprocessing Course

Download your FREE multiprocessing PDF cheat sheet and get BONUS access to my free 7-day crash course on the multiprocessing API.

Discover how to use the Python multiprocessing module including how to create and start child processes and how to use a mutex locks and semaphores.

Learn more
 


Further Reading

This section provides additional resources that you may find helpful.

Python Multiprocessing Books

I would also recommend specific chapters in the books:

Guides

APIs

References

    Takeaways

    You now know the difference between parent and child processes in Python.

    Do you have any questions about the difference between Parent and Child in Python?
    Ask your questions in the comments below and I will do my best to answer.

    Photo by Rodolfo Flores on Unsplash