Python threading get_ident() Method with Example

Python threading.get_ident() Method: In this tutorial, we will learn about the get_ident() method of threading module in Python with its usage, syntax, and examples. By Hritika Rajput Last updated : April 23, 2023

Python threading.get_ident() Method

The threading.get_ident() is an inbuilt method of the threading module, it is used to return the "thread identifier" of the current thread. Thread identifiers can be recycled when a thread exits and another thread is created. The value has no direct meaning.

Module

The following module is required to use get_ident() method:

import threading

Syntax

The following is the syntax of get_ident() method:

get_ident()

Parameter(s)

The following are the parameter(s):

  • None

Return Value

The return type of this method is <class 'int'>, it returns a number that acts as a thread identifier for the current thread.

Example of threading.get_ident() Method in Python

# Python program to explain the use of 
# get_ident() method in Threading Module

import time
import threading

def thread_1(i):
    time.sleep(5)
    print("Thread identifier for Thread-1:", threading.get_ident())
    print('Value by Thread 1:', i)

def thread_2(i):
    print("Thread identifier for Thread-2:", threading.get_ident())
    print('Value by Thread 2:', i)
    
def thread_3(i):
    time.sleep(4)
    print("Thread identifier for Thread-3:", threading.get_ident())
    print("Value by Thread 3:", i)    

# Creating sample threads 
thread1 = threading.Thread(target=thread_1, args=(10,))
thread2 = threading.Thread(target=thread_2, args=(20,))
thread3 = threading.Thread(target=thread_3, args=(30,))

print("Thread identifier for main-thread:", threading.get_ident())

# Starting the threads
thread1.start()
thread2.start()
thread3.start()

Output

Thread identifier for main-thread: 139909786703616
Thread identifier for Thread-2: 139909752215296
Value by Thread 2: 20
Thread identifier for Thread-3: 139909743822592
Value by Thread 3: 30
Thread identifier for Thread-1: 139909760608000
Value by Thread 1: 10



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.