'threading.enumerate()'
method return list of currently live thread objects.
MyThread.py
import threading import time class MyThread(threading.Thread): def run(self): print(threading.current_thread().getName()," with id ",threading.get_ident()," Started") time.sleep(2) 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() active_threads = threading.enumerate(); for thread in active_threads: print("Active Thread : ", thread.getName()) time.sleep(3) active_threads = threading.enumerate(); for thread in active_threads: print("Active Thread : ", thread.getName())
$ python3 MyThread.py Thread_1 with id 4318040064 Started Thread_2 with id 4333768704 Started Thread_3 with id 4339023872 Started Active Thread : MainThread Active Thread : Thread_1 Active Thread : Thread_2 Active Thread : Thread_3 Thread_1 with id 4318040064 Finished Thread_2 with id 4333768704 Finished Thread_3 with id 4339023872 Finished Active Thread : MainThread
No comments:
Post a Comment