The name property of the Thread class

A Python thread instance can be given a name using the property "name". The semantic of this name is up to the developer of the program.

Thread Updated 21 Nov 2017 1 min read

Overview

  • This property of the Thread class allows a Python Thread instance to be a given a name.
  • Generally developers would like to identify threads created from their programs with some sort of naming scheme. The name property comes handy here.
  • Though multiple threads can be given same name by the developer it is up to the developer to give meaningful names for one or more threads.
  • The following Python Program creates a thread and gives its name CustomThread followed by a numeric suffix.

Example

python
from threading import Thread

def ThreadFunction():

    print("Inside thread Body")

customThread = Thread(target=ThreadFunction())

customThread.name = "CustomThread{}".format(1)

print(customThread.name)

customThread.start()

customThread.join()

print("Main thread resumed")

print("Main thread exiting")

Output

Output
Inside thread Body

CustomThread1

Main thread resumed

Main thread exiting