There are two ways to implement Python Multithreading:
Functions, Thread classes
1. function
Call the Start_new_thread () function in the thread module to create a thread that tells the thread what to do in the form of a thread function
Copy Code code as follows:
#-*-Coding:utf-8-*-
Import Thread
def f (name):
#定义线程函数
Print "This is" + Name
if __name__ = = ' __main__ ':
Thread.start_new_thread (F, ("Thread1",))
#用start_new_thread () calling thread functions and other parameters
While 1:
Pass
However, this method has not been able to find other auxiliary methods for the time being, even the main thread wait to be solved with the while 1 method.
2. Thread class
Call the threading module and create the threading. A subclass of thread to get a custom thread class.
Copy Code code as follows:
#-*-Coding:utf-8-*-
Import threading
Class Th (threading. Thread):
def __init__ (self, name):
Threading. Thread.__init__ (self)
Self.t_name = Name
#调用父类构造函数
def run (self):
#重写run () function, which the thread defaults to start from this function
Print "This is" + self.t_name
if __name__ = = ' __main__ ':
Thread1 = Th ("Thread_1")
Thread1.start ()
The #start () function starts the thread and automatically executes the run () function
Threading. Inheritable functions of the Thread class:
GetName () Get the Thread object name
SetName () Set the thread object name
Join () The command to wait for the calling thread to finish before running
Setdaemon (BOOL) blocking mode, True: The parent thread does not wait for the child thread to end, false waits, and defaults to False
Isdaemon () Determines whether the child thread ends with the parent thread, that is, the value set by Setdaemon ()
IsAlive () to determine if the thread is running
Instance
Copy Code code as follows:
Import threading
Import time
Class Th (threading. Thread):
def __init__ (self, thread_name):
Threading. Thread.__init__ (self)
Self.setname (Thread_name)
def run (self):
Print "This is thread" + self.getname ()
For I in range (5):
Time.sleep (1)
Print str (i)
Print self.getname () + "are over"
Join () Blocking wait
Copy Code code as follows:
if __name__ = = ' __main__ ':
Thread1 = Th ("T1")
Thread1.start ()
#thread1. Join ()
Print "Main thread is over"
Without Thread1.join (), the following results are obtained:
Copy Code code as follows:
This is thread T1
Main thread is over
0
1
2
T1 is Over
Do not wait for Thread1 to complete, after execution of the statement.
Add the Thread1.join () and get the following results:
Copy Code code as follows:
This is thread T1
0
1
2
T1 is Over
Main thread is over
Block waits for Thread1 to finish before executing the following statement
Main thread Wait
Copy Code code as follows:
if __name__ = = ' __main__ ':
Thread1 = Th ("T1")
Thread1.setdaemon (True)
#要在线程执行之前就设置这个量
Thread1.start ()
Print "Main thread is over"
Error: Exception in thread T1 (most likely raised during):
That is, the main thread does not wait for child threads to end.
Multiple Child threads
Copy Code code as follows:
if __name__ = = ' __main__ ':
For I in range (3):
t = Th (str (i))
T.start ()
Print "Main thread is over"
Here the t can handle multiple threads at the same time, that is, T is a thread handle, and the reassign does not affect the thread.
Oddly, when you run T.run (), no more threads are executed. Although it is unclear, use the start (). The idea is that start () is non-blocking and parallel, and run is blocked.
Thread Lock
Threading provides a thread lock, which enables synchronization of threads.
Copy Code code as follows:
Import threading
Import time
Class Th (threading. Thread):
def __init__ (self, thread_name):
Threading. Thread.__init__ (self)
Self.setname (Thread_name)
def run (self):
Threadlock.acquire ()
#获得锁之后再运行
Print "This is thread" + self.getname ()
For I in range (3):
Time.sleep (1)
Print str (i)
Print self.getname () + "are over"
Threadlock.release ()
#释放锁
if __name__ = = ' __main__ ':
Threadlock = Threading. Lock ()
#设置全局锁
Thread1 = Th (' thread_1 ')
Thread2 = Th (' thread_2 ')
Thread1.start ()
Thread2.start ()
Get the result:
Copy Code code as follows:
This is thread thread_1
0
1
2
Thread_1 is Over
This is thread thread_2
0
1
2
Thread_2 is Over