Python

Python Gets thread id

The “thread” in Python is an instruction set that can be executed independently to accomplish a specific task. The thread id is a particular identifier assigned or set to each thread that is currently running in a program. To get thread id, various methods are utilized in Python.

This Python post presents a comprehensive guide on how to get thread id via the following methods:

Method 1: Get Thread ID in Python Using “threading.get_ident()” Method

The “threading.get_ident()” method of the “threading” module is used to get the thread identifier of the present/current thread. Here is an example to retrieve the thread id:

import time
import threading
def funct():
    time.sleep(2)
    print(threading.get_ident(), "Thread in action")
thread1= threading.Thread(target=funct)
thread2= threading.Thread(target=funct)
thread1.start()
thread2.start()

 

Here in this code:

  • First of all, import the “threading” and “time” modules.
  • Then, define the user-defined function named “funct()” in the program.
  • Next, used the “time.sleep()” to introduce a delay to simulate some processing.
  • After the delay, apply the “threading.get_ident()” method to print the thread identifier along with the message.
  • Then, utilized the “threading.Thread()” method to create the two thread objects.
  • Lastly, invoked the “start()” method to call/access the thread object to begin the thread’s execution.
  • The “thread1” and “thread2” will execute the user-defined function concurrently.

As you can see, the thread identifier, along with the specified message displayed on the console:

Method 2: Get Thread ID in Python Using “threading.get_native_id()” Method

The “threading.get_native_id()” method of the “threading” module is used to retrieve the non-zero integer native Thread identifier of the present thread, which is set by the kernel. Let’s understand this method via the following example code:

import time
import threading
def funct():
    time.sleep(2)
    print(threading.get_native_id(), "Thread in action")
thread1= threading.Thread(target=funct)
thread2= threading.Thread(target=funct)
thread1.start()
thread2.start()

 

In the above-given example:

  • We used the “threading.get_native_id()” method instead of the “threading.get_ident()” method to get the thread identifier along with the specified message.
  • The rest of the code is similar to the previous/last examples.

Output

The unique identifier is returned for each thread in the above output.

Method 3: Get Thread ID in Python Utilizing the “Logging” Module

The logging module can also be used to get/retrieve the thread identifier in Python. Let’s take the following example code that demonstrates the working of this particular method for better understanding:

import logging
import time
import threading

def thread_function():
    logging.info("Thread in action")
    time.sleep(2)

logging.basicConfig(format="%(thread)d : %(message)s ", level=logging.INFO)
t1 = threading.Thread(target=thread_function)
t2 = threading.Thread(target=thread_function)
t1.start()
t2.start()

 

According to the above-provided code:

  • Initially, imported the required modules “logging”, “threading”, and “time”, respectively.
  • Then, applied the “logging.basicConfig()” method to set up the default handler.
  • Next, we used the “%(thread)d” mapping key along with the “%(message)s” mapping key to display the thread identifier and log message.

Output

The thread identifier is shown in the above snippet.

Conclusion

In Python, the “threading.get_ident()” method, “threading.get_native_id()” method, and “Logging” module are used to get the thread id. The “threading.get_ident()” and “threading.get_native_id()” methods retrieved the thread identifier along with the specified message. The logging module can also be used to add/insert the thread identifier in the log message. This post presented detailed information on retrieving thread IDs using numerous examples.

About the author

Haroon Javed

Hi, I'm Haroon. I am an electronics engineer and a technical content writer. I am a tech geek who loves to help people to the best of my knowledge.