################# Thread Demo Script #######################
#coding =utf-8
Import threading
From time import Ctime,sleep
def music (func):
For I in range (2):
Print "I was listening to%s.%s"% (Func,ctime ())
Sleep (1)
def move (func):
For I in range (2):
Print "I am at the%s! %s "% (Func,ctime ())
Sleep (5)
Threads = []
T1 = Threading. Thread (target=music,args= (U ' Love Trade ',)) # Threading. Thread (function, args[, Kwargs])
Threads.append (T1)
T2 = Threading. Thread (target=move,args= (U ' Avatar ',))
Threads.append (T2)
if __name__ = = ' __main__ ':
For T in Threads:
T.setdaemon (True) # declares a thread as a daemon thread, and when the main thread ends it kills the child thread if the child thread does not end
T.start ()
For T in thread:
T.join () # After the child thread execution is complete, the main thread can then be executed down
Print "All over%s"%ctime ()
################# Thread Demo Script #######################
Import threading
First, import the threading module, which is the premise of using multithreading.
Threads = []
T1 = Threading. Thread (target=music,args= (U ' Love Trade ',))
Threads.append (T1)
Creates a threads array, creates thread T1, and uses threading. The Thread () method, in which the music method is called by the Target=music,args method to pass a parameter to the music. Load the created thread T1 into the threads array.
The thread T2 is then created in the same way, and the T2 is also loaded into the threads array.
For T in Threads:
T.setdaemon (True)
T.start ()
Finally, the array is traversed by a for loop. (the array is loaded with T1 and T2 two threads)
Setdaemon ()
Setdaemon (True) declares a thread as a daemon thread and must be set before the start () method call, if not set to the daemon, is indefinitely suspended. After the child thread is started, the parent thread continues to execute, and when the parent thread finishes executing the last statement print "All over%s"%ctime (), it exits without waiting for the child thread, and the child threads end together.
Start ()
Starts the thread activity.
Python Threads use