Python threading setName














































Python threading setName



setName( ) 



This method sets the name of a thread.


Python threading module provides a current_thread() function which returns the reference to the current executing thread object. Using this thread object, it is possible to set and get the name of executing thread by calling setName and getName methods on the thread object respectively.

The name is a string used for identification purposes only. It has no semantics. Multiple threads may be given the same name. The initial name is set by the constructor.
source code:


import threading

import time


def Thread1():

    print ('Starting  ',threading.currentThread().getName())

    time.sleep(2)

    print ('Exiting  ',threading.currentThread().getName())


def Thread2():

    print ( 'Starting  ',threading.currentThread().getName())

    time.sleep(3)

    print ('Exiting  ',threading.currentThread().getName())


t1 = threading.Thread(target=Thread2)
t2 = threading.Thread(target=Thread1)
t2.start()
t2.setName('new thread2 ')
t1.start()
t1.setName('new thread1 ')



output:



Comments