Monday, 14 December 2015

Python: Get identifier of current thread

‘threading.get_ident()’  method return the identifier of current thread.


MyThread.py
import threading
import time

class MyThread(threading.Thread):
 def run(self):
  print(threading.current_thread().getName()," with id ",threading.get_ident()," Finished")

thread1 = MyThread(name="Thread_1")
thread2 = MyThread(name="Thread_2")
thread3 = MyThread(name="Thread_3")

thread1.start()
thread2.start()
thread3.start()

$ python3 MyThread.py 
Thread_1  with id  4318040064  Finished
Thread_2  with id  4318040064  Finished
Thread_3  with id  4333768704  Finished



Previous                                                 Next                                                 Home

No comments:

Post a Comment