First, need to understand the method
Methods for thread instance objects
# isAlive (): Determine if this thread is alive
# getName (): Get thread Name
# SetName (): Set thread name
#enumerate (): View Active Process objects
#activeCount (): View the number of active threads
From threading Import Thread,activecount,enumerate,current_threadimport timedef Task (): print ('%s is running '% Current_thread (). GetName ()) Time.sleep (2) if __name__ = = ' __main__ ': t=thread (Target=task,) T.start () t.join () print (T.is_alive ()) #判断这个线程是否是存活的 print (T.getname ()) #获取线程名 print (Enumerate ()) # View Active Process Object print (' master ') print (Activecount ()) #查看活跃的线程数
Second, the use of current_thread (* * * * *)
You can use the Current_thread method to call some other methods, the test is often used to
To see what the open thread names are.
From threading import Thread, Activecount, enumerate, current_threadfrom multiprocessing import processimport timedef tas K (): print ('%s is running '% current_thread (). GetName ()) #可以用current_thread方法去调用一些其它方法, testing is often used to time.sleep ( 2) If __name__ = = ' __main__ ': p = Process (target=task) p.start () print (Current_thread ())
Third, based on the current_thread to verify that a process is open, the default is a main thread
Emphasis: The main thread represents the execution process of the process from the execution level.
From threading import Thread, Activecount, enumerate, current_threadfrom multiprocessing import processimport timedef tas K (): print ('%s is running '% current_thread (). GetName ()) Time.sleep (2) if __name__ = = ' __main__ ': t1 = Thread (target=task) t2 = thread (target=task) t3 = Thread (target=task) T1.start () T2.start () T3.start () print (Current_thread (). GetName ()) #在一个进程里边, the main thread is only one, the others are child threads
day10-04_ Multithreading Common Attribute method