I. Overview
import threading
调用 t1 = threading.Thread(target=function , args=(,))
Examples of the method of the Thread class
# Join (): Before the child thread finishes running, the parent of the child thread thread will be blocked. SetDaemon # (True): '' ' the statement thread as a daemon thread must be set before the method is called start (), if you do not set a daemon thread hangs the program will be unlimited. When we are running, perform a main thread, if the main thread and create a child thread, the main thread and the child thread on the two forces were divided, each running, then when the main thread is completed when you want to exit, it will test the child thread is complete. If the child thread is not completed, the main thread will wait for the child threads to complete before exit. But sometimes we need is just the main thread completed, regardless of whether the child thread to complete, and should be the main thread exits together, then you can use it setDaemon method '' ' Import Threading from Time Import ctime, SLEEP Import Time DEF Music (name): Print (.. "the Begin Listening to {name} {Time}" the format (name = name, Time = the ctime ())) SLEEP (. 3) . Print ( "End Listening {Time}" the format (Time = ctime ())) DEF Blog (title): Print ( "the Begin Recording The {title}. {Time}." the format (title = title, Time = the ctime ())) SLEEP (. 5) Print ( 'End Recording {Time}'. the format (Time = the ctime ()) ) Threads = [] T1 = of the threading.Thread (= Music target, args = ( 'FILL the ME',)) T2 = of the threading.Thread (target = Blog, args = ( '',)) threads.append (T1) Threads .append (T2) IF the __name__ == '__main__': # t2.setDaemon (True) for T in Threads: # t.setDaemon (True) # NOTE: Be sure to set before the Start t.start () # t.join ( ) # t1.join () # t2.join () # account that under these three join position? print ( "all over% s" % ctime ())
Other examples of methods
Thread instance of an object method
# isAlive (): Returns the thread is active. # GetName (): Returns the thread name. # SetName (): Set the thread name. threading module provides a number of methods: # threading.currentThread (): Returns the current thread variable. # Threading.enumerate (): Returns a list of running threads. Refers to the thread starts running, before the end, it does not include a thread before starting and after termination. # Threading.activeCount (): Returns the number of running threads, and len (threading.enumerate ()) have the same result.