Python threading Module | get_ident() Method with Example

Python threading.get_ident() Method: Here, we are going to learn about the getName() method of threading Module in Python with its definition, syntax, and examples.
Submitted by Hritika Rajput, on May 14, 2020

Python threading.get_ident() Method

get_ident() is an inbuilt method of the threading module in Python. 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:

    import threading

Syntax:

    get_ident()

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:

# 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

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.