Other properties or methods of the thread object
Introduced:
Thread instance object Method # isAlive (): Returns the thread is active # getName (): Returns the thread name # setnmae (): Sets the thread name threading module provides some methods: # Threading.currentthread (): returns the current thread variable # threading.enumerate (): Returns a list containing the running thread. Running refers to threads that do not include pre-and post-termination threads until after the thread has started and ends. # Threading.activecount (): Returns the number of running threads with the same result as Len (Threading.enumerate ())
Verify
From threading import threadimport threadingfrom multiprocessing import processimport osimport timedef work (): Time.sleep (3) print (Threading.current_thread (). GetName ()) If __name__ = = ' __main__ ': # Open thread under main process T = Thread (target=work) t.start () print (Threading.current_thread (). GetName ()) print (Threading.current_ Thread ()) # main thread print (Threading.enumerate ()) # with two running threads in the main thread print (Threading.active_count ()) print (' main thread/main process ')
Execution results
Mainthread<_mainthread (Mainthread, started 140037784917824) >[<_mainthread (Mainthread, started 140037784917824), <thread (Thread-1, started 140037751482112) >]2 main thread/main process Thread-1
The main thread waits for the child thread to end
Import timedef Sayhi (name): time.sleep (2) print ('%s say hello '% name) if __name__ = = ' __main__ ': t = Thread (t Arget=sayhi, args= (' Mike ',)) T.start () t.join () print (' main thread ') print (t.is_alive ())
Execution results
Mike say hello Main thread false
Python concurrent Programming: Multithreading-thread other properties and methods of objects