How to Change the Process Name in Python
You can set the process name in the multiprocessing.Process class constructor or via the "name" property of the multiprocessing.Process class.
In this tutorial you will discover how to change the name of processes in Python.
Let's get started.
Need to Change Process Name
A process is a running instance of a computer program.
Every Python program is executed in a Process, which is a new instance of the Python interpreter. This process has the name MainProcess and has one thread used to execute the program instructions called the MainThread. Both processes and threads are created and managed by the underlying operating system.
Sometimes we may need to create new child processes in our program in order to execute code concurrently.
Python provides the ability to create and manage new processes via the multiprocessing.Process class.
You can learn more about multiprocessing in the tutorial:
In concurrent programming, we may need to assign meaningful names to new processes.
This may be for many reasons such as:
- To differentiate processes while debugging.
- To differentiate processes while logging.
- To execute code conditionally on the process name.
How can we set and change process names in Python?
How to Get the Process Name
We can get the name of a process via the "name" property on the multiprocessing.Process class.
For example:
...
# report the name of the process
print(process.name)
We may get a multiprocessing.Process instance by creating a new process ourselves.
For example:
...
# create a process
process = Process()
# report the name of the process
print(process.name)
Or we may acquire an instance of the multiprocessing.Process for the current process via the multiprocessing.current_process() function.
For example:
...
# get the current process instance
process = current_process()
# report the name of the process
print(process.name)
It may be helpful to get the multiprocessing.Process instance for the current process from within a function or code executing in a new process so that the new process can report properties about itself, such as its name.
Now that we know how to get the name of a process, let's look at how we might change its name.
How to Change the Process Name
The name of a process can be set when creating a new process.
The parent process will always have the name "MainProcess".
The name of new child processes will default to the format "Process-%d", where "%d" is an integer for the process number created by the parent process, counted from 1 sequentially up for each new child process. This means that the first new process you might create would have the name "Process-1".
When creating a new instance of a multiprocessing.Process, we can specify the name of the process via the "name" argument.
For example:
...
# create a process with a custom name
process = Process(name='MyProcess')
We can also configure the process name after the multiprocessing.Process instance has been created via the "name" property.
For example:
...
# create a process with default name
process = Process()
# set the name
process.name = 'MyProcess'
Now that we know how to change the process name, let's look at some worked examples.
Example of Getting the Current Process Name
We can explore how to get the name of the current process.
First, we must acquire the multiprocessing.Process instance for the current process.
This can be achieved via the multiprocessing.current_process() function.
...
# get the current process
process = current_process()
Once acquired, we can report the name of the process via the "name" attribute.
...
# report a message
print(f'Hello from process: {process.name}')
Tying this together, the complete example is listed below.
# SuperFastPython.com
# example of getting the current process name
from multiprocessing import current_process
# get the current process
process = current_process()
# report a message
print(f'Hello from process: {process.name}')
Running the example first acquires the multiprocessing.Process instance for the current process and then reports the name.
In this case, we can see that the name of the current process is "MainProcess" to indicate the main process which is the default for the Python parent process.
Hello from process: MainProcess
Next, let's look at how we might get the name of a new process.
Example of Getting the Process Name
We can explore how to get the name of a new process.
First, we can define a function that will execute in a new process.
We will name the function task() and it will take no arguments.
# function executed in a new process
def task():
# ...
Within the function, we will first get the multiprocessing.Process instance for the process.
...
# get the current process
process = current_process()
Then we can report a message indicating the name of the new process executing our function.
...
# report a message
print(f'Hello from a new process: {process.name}')
Tying this together, the complete task() function is listed below.
# function executed in a new process
def task():
# get the current process
process = current_process()
# report a message
print(f'Hello from a new process: {process.name}')
Next, in the parent process we can create a new multiprocessing.Process instance and configure it to run our new task() function via the "target" attribute.
...
# create a new process
process = Process(target=task)
We can call the start() method on the new process which will begin executing the new child process, in turn calling the internal run() function and finally calling our task() function.
...
# start the new process
process.start()
Finally, the parent process can block until the child process is finished.
...
# wait for the new process to terminate
process.join()
Tying this together, the complete example of getting the process name is listed below.
# SuperFastPython.com
# example of getting the process name
from multiprocessing import Process
from multiprocessing import current_process
# function executed in a new process
def task():
# get the current process
process = current_process()
# report a message
print(f'Hello from a new process: {process.name}')
# entry point
if __name__ == '__main__':
# create a new process
process = Process(target=task)
# start the new process
process.start()
# wait for the new process to terminate
process.join()
Running the example first creates our new process instance then executes it.
The new process runs our task() function first acquiring the multiprocessing.Process instance of the new process executing the function, then reporting its name.
In this case, we can see that the new process used to execute our function had the default name "Process-1" for the first new child process within the Python parent process.
Hello from a new process: Process-1
Next, let's look at how we might set a name for a new process.
Example of Setting the Process Name
We can explore how to set the name for a new process.
The name for a new process can be set via the "name" argument in the constructor to the multiprocessing.Process class when creating a new process.
We can update the example from the previous section with a task() function that gets the process instance for the process executing the function then reports the name of the process.
First, we can set the name when constructing the new process instance.
...
# create a new process
process = Process(target=task, name='Worker')
Then the process can be started as before by calling the start() function.
...
# start the new process
process.start()
Tying this together, the complete example of setting the process name is listed below.
# SuperFastPython.com
# example of setting the process name
from multiprocessing import Process
from multiprocessing import current_process
# function executed in a new process
def task():
# get the current process
process = current_process()
# report a message
print(f'Hello from a new process: {process.name}')
# entry point
if __name__ == '__main__':
# create a new process
process = Process(target=task, name='Worker')
# start the new process
process.start()
# wait until the new process has terminated
process.join()
Running the example creates the new multiprocessing.Process instance configured with a custom name and or task() function as the target function.
The process is started and the task() function acquires the process instance and reports the name, matching the name we assigned in the constructor to the class.
Hello from a new process: Worker
Next, let's look at how we might change the name of a process after it has been created.
Example of Changing the Process Name
We can explore how to change the name of a process after it has been created.
We can update the example from the previous section and change it so that the new name of the process is assigned after it is created instead of in the constructor.
We will use the same task() function that gets the process instance and reports the name of the process executing the function.
First, we can create the process as per normal, configured to execute our custom task() function.
...
# create a new process
process = Process(target=task)
Next, we can set a custom name for the process via the "name" property.
...
# set a custom name for the new process
process.name = 'Worker'
Finally, we can start the new process as before.
...
# start the new process
process.start()
Tying this together, the complete example of changing the process name is listed below.
# SuperFastPython.com
# example of setting the process name via the property
from multiprocessing import Process
from multiprocessing import current_process
# function executed in a new process
def task():
# get the current process
process = current_process()
# report a message
print(f'Hello from a new process: {process.name}')
# entry point
if __name__ == '__main__':
# create a new process
process = Process(target=task)
# set a custom name for the new process
process.name = 'Worker'
# start the new process
process.start()
# wait until the new process has terminated
process.join()
Running the example creates the process, sets a new name for the process, then starts executing.
The new process runs the task() function that gets the current process instance and reports the process name.
We can see that the name of the process reported matches the name that we set after the process was created.
Hello from a new process: Worker
Next, let's look at an example of changing the process name after the process is already running.
Example of Changing the Process Name While Running
We can explore how to change the name of a process after the process is already running.
We will update the example from the previous section that sets the name of the process after the process was constructed. We can then update the task() function to get the process instance and report this name, then change the name and report the new name.
First, let's update the task() function.
# function executed in a new process
def task():
# ...
The task function is defined as before, first getting the multiprocessing.Process instance and reporting the custom name.
...
# get the current process
process = current_process()
# report a message
print(f'Hello from a new process: {process.name}')
Next, from within the task() function that will be executed by the process, will change the name to 'BestWorker', then report this new name.
...
# change the name (while running)
process.name = 'BestWorker'
# report a message
print(f'Hello again, this time from: {process.name}')
Tying this together, the complete updated task() function is listed below.
# function executed in a new process
def task():
# get the current process
process = current_process()
# report a message
print(f'Hello from a new process: {process.name}')
# change the name (while running)
process.name = 'BestWorker'
# report a message
print(f'Hello again, this time from: {process.name}')
Finally, we can create the new process, set a custom name and start it as we did previously.
...
# create a new process
process = Process(target=task)
# set a custom name for the new process
process.name = 'Worker'
# start the new process
process.start()
# wait until the new process has terminated
process.join()
Tying this together, the complete example of changing the process name while the process is running is listed below.
# SuperFastPython.com
# example of setting the process name via the property
from multiprocessing import Process
from multiprocessing import current_process
# function executed in a new process
def task():
# get the current process
process = current_process()
# report a message
print(f'Hello from a new process: {process.name}')
# change the name (while running)
process.name = 'BestWorker'
# report a message
print(f'Hello again, this time from: {process.name}')
# entry point
if __name__ == '__main__':
# create a new process
process = Process(target=task)
# set a custom name for the new process
process.name = 'Worker'
# start the new process
process.start()
# wait until the new process has terminated
process.join()
Running the example first creates a new process, configures it with a custom name then starts executing our custom task() function.
The task() function first acquires the multiprocessing.Process instance of the process that is executing itself and reports the process name. This matches the name assigned after the multiprocessing.Process instance was created.
Next, the task() function changes the name of the process, while it is running, and reports the new name. This matches the changed name.
This example shows that we can dynamically change the name of a process while it is running, from within code that the process is executing.
Hello from a new process: Worker
Hello again, this time from: BestWorker
Takeaways
You now know how to change the name of processes in Python.
If you enjoyed this tutorial, you will love my book: Python Multiprocessing Jump-Start. It covers everything you need to master the topic with hands-on examples and clear explanations.