Python threading get_ident Function

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

The threading.get_ident function in Python's threading module returns the "thread identifier" of the current thread. This identifier is a non-zero integer that uniquely identifies each thread during its lifetime. It can be useful for debugging and logging purposes.

Table of Contents

  1. Introduction
  2. threading.get_ident Function Syntax
  3. Examples
    • Basic Usage
    • Using with Thread Creation
    • Comparing Thread Identifiers
  4. Real-World Use Case
  5. Conclusion

Introduction

The threading.get_ident function provides a unique identifier for the current thread. This can be useful for tracking threads and distinguishing between them, especially when debugging or logging thread activity.

threading.get_ident Function Syntax

Here is how you use the threading.get_ident function:

import threading

thread_id = threading.get_ident()

Returns:

  • A non-zero integer representing the unique identifier of the current thread.

Examples

Basic Usage

Get and print the identifier of the current thread.

Example

import threading

def worker():
    thread_id = threading.get_ident()
    print(f"Worker thread ID: {thread_id}")

worker_thread = threading.Thread(target=worker)
worker_thread.start()
worker_thread.join()

main_thread_id = threading.get_ident()
print(f"Main thread ID: {main_thread_id}")

Output:

Worker thread ID: 123145321082368
Main thread ID: 140735282373568

Using with Thread Creation

Track the identifiers of multiple threads.

Example

import threading

def worker():
    thread_id = threading.get_ident()
    print(f"Worker thread ID: {thread_id}")

threads = []
for i in range(3):
    thread = threading.Thread(target=worker)
    threads.append(thread)
    thread.start()

for thread in threads:
    thread.join()

main_thread_id = threading.get_ident()
print(f"Main thread ID: {main_thread_id}")

Output:

Worker thread ID: 123145321082368
Worker thread ID: 123145321082368
Worker thread ID: 123145321082368
Main thread ID: 140735282373568

Comparing Thread Identifiers

Compare the identifiers of the current thread and the main thread.

Example

import threading

def worker():
    worker_thread_id = threading.get_ident()
    main_thread_id = threading.main_thread().ident
    if worker_thread_id == main_thread_id:
        print("Worker is running on the main thread")
    else:
        print("Worker is running on a worker thread")

worker_thread = threading.Thread(target=worker)
worker_thread.start()
worker_thread.join()

main_thread_id = threading.get_ident()
print(f"Main thread ID: {main_thread_id}")

Output:

Worker is running on a worker thread
Main thread ID: 140735282373568

Real-World Use Case

Logging Thread Activity

Use threading.get_ident to log the activity of different threads.

Example

import threading
import logging

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(message)s')

def worker():
    thread_id = threading.get_ident()
    logging.debug(f"Worker thread ID: {thread_id} is running")
    logging.debug(f"Worker thread ID: {thread_id} is doing some work")
    logging.debug(f"Worker thread ID: {thread_id} has finished work")

threads = []
for i in range(3):
    thread = threading.Thread(target=worker)
    threads.append(thread)
    thread.start()

for thread in threads:
    thread.join()

main_thread_id = threading.get_ident()
logging.debug(f"Main thread ID: {main_thread_id} is exiting")

Output:

2023-07-25 12:00:00,000 Worker thread ID: 123145321082368 is running
2023-07-25 12:00:00,001 Worker thread ID: 123145321082368 is doing some work
2023-07-25 12:00:00,002 Worker thread ID: 123145321082368 has finished work
2023-07-25 12:00:00,003 Worker thread ID: 123145321082368 is running
2023-07-25 12:00:00,004 Worker thread ID: 123145321082368 is doing some work
2023-07-25 12:00:00,005 Worker thread ID: 123145321082368 has finished work
2023-07-25 12:00:00,006 Worker thread ID: 123145321082368 is running
2023-07-25 12:00:00,007 Worker thread ID: 123145321082368 is doing some work
2023-07-25 12:00:00,008 Worker thread ID: 123145321082368 has finished work
2023-07-25 12:00:00,009 Main thread ID: 140735282373568 is exiting

Conclusion

The threading.get_ident function is used for obtaining the unique identifier of the current thread in Python. It can be used for debugging, logging, and tracking thread activity, providing a clear way to distinguish between different threads in your application. Proper usage can enhance the visibility and manageability of your multithreaded programs.

My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare