Python Thread-based parallelism : current_thread()














































Python Thread-based parallelism : current_thread()



Python threading.current_thread() Method


current_thread() is an inbuilt method of the threading module in Python. It is used to return the current Thread object, which corresponds to the caller's thread of control. If  the caller%u2019s thread of control was not created through the  threading module, a dummy thread object with limited functionality is returned.

Module:

                     import threading

Syntax:

                    current_thread()

Return value:

The return type of this method is a Thread class object, it returns the current Thread object active at the moment.

How to get the current thread ?(example 1)

import threading
thread = threading.currentThread()
print(thread)

Output :-

                      <_MainThread(MainThread, started 24632)>

Python program to explain the use of current_thread() method in Threading Module(example 2)

import time
import threading

def thread_1(i):
time.sleep(2)
print("Active current thread right now:", (threading.current_thread()))
print('Value by Thread 1:', i)

def thread_2(i):
time.sleep(5)
print("Active current thread right now:", (threading.current_thread()))
print('Value by Thread 2:', i)

def thread_3(i):
print("Active current thread right now:", (threading.current_thread()))
print("Value by Thread 3:", i)

# Creating sample threads
thread1 = threading.Thread(target=thread_1, args=(1,))
thread2 = threading.Thread(target=thread_2, args=(2,))
thread3 = threading.Thread(target=thread_3, args=(3,))

print("Active current thread right now:", (threading.current_thread()))
#3 Initially it is the main thread that is active
# Starting the threads
thread1.start()
thread2.start()
thread3.start()


OUTPUT :-



In next article, we will learn about  threading.excepthook 

Link -  javascript:nicTemp();


More Articles of Diksha Kumari:

Name Views Likes
Python : py_compile | Generate byte code 308 0
Python : py_compiler |Convert py to pyc 1157 0
Python : py_compile |Compile python sources file 287 0
Python :How to lock Critical Sections(Thread based parallelism) 249 0
Python : Locking without Deadlocks(Thread based Parallelism ) 180 0
Python : Communicating Between Threads-2(Thread based parallelism) 389 0
Python : Communicating Between Threads -1(Thread based parallelism) 176 0
Python Start and Stop a thread (Thread based parallelism) 165 0
Python: Check if a Thread has started (thread based parallelism) 144 0
Python Thread-based parallelism :main_thread() 229 0
Python Lock Class | acquire() Method(thread based parallelism) 294 0
Python Thread-based parallelism : get_native_id() 460 0
Python Thread-based parellelism: active_count() 269 1
Python Lock Class | lock() Method(thread based parallelism) 246 0
Python Thread-based parallelism : stack_size([size]) 331 1
Python Thread Local Data(thread based parallelism 265 1
Python Semaphore Objects (thread based parallelism) 264 0
Python Event class - set() method | Thread based parallelism 252 1
Python Condition object (thread based parallelism) 411 0
Python Thread based parallelism : Run() 228 0
Python Thread-based parallelism : current_thread() 254 1
Python Condition object - acquire()(thread based parallelism) 190 0
Python Thread-based parallelism : TIMEOUT_MAX () 423 0
Python Condition object - notify_all() ( thread based parallelism ) 192 0
Python Thread-based parallelism :.settrace(func) 308 0
Python difference lock and RLock object : Thread based parallelism 1082 0
Python Event class - clear() method | Thread based parallelism 188 0
Python Thread object -start() (thread based parallelism) 335 0
Python RLock Class - acquire() Method (thread based parallelism) 226 0
Python Thread-based parallelism : enumerate() 246 0
Python Thread based parallelism: get_ident() 302 1
Python Thread-based parallelism : setprofile(func) 215 0
Python RLock Class - release() Method (thread based parallelism) 194 0
Python Barrier object | Thread based parallelism 228 0
Python Condition object - notify () ( thread based parallelism ) 217 0
Python Event class - is_set() method | Thread based parallelism 173 0
Python Thread based parallelism : join() 317 0
Python thread based parallelism : set.name() 200 0
Python Event class - wait() method | Thread based parallelism 243 0
PYTHON Thread based parallism 292 1
Python Barrier method | Thread based parallelism 228 0
Python Condition object - release() ( thread based parallelism ) 184 0
Python Thread based parallelism: excepthook 1487 1
Python thread object : is_alive() (thread based parallelism) 996 0
Python Condition object - wait() ( thread based parallelism ) 215 0
Python Lock Class | release() Method(thread based parallelism) 254 0
Python Thread object (thread based parallelism) 259 0
Python Timer class - start() method | Thread based parallelism 175 1
Python thread based parallelism : get.name() 322 1
Python Timer class - cancel() method | Thread based parallelism 206 1
Python thread object : Deamon() (thread based parallelism) 241 0

Comments