There are two ways to implement multithreading in Python: function, Thread Class 1. The function calls 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
#-*-Coding:utf-8-*-import threaddef F (name): #定义线程函数 print "This is" + name if __name__ = = ' __main__ ': th Read.start_new_thread (F, ("Thread1",)) #用start_new_thread () call thread functions and other parameters while 1: Pass
However, this method has not been able to find other auxiliary methods, even the main thread waits to use while 1 this method to solve. 2. Thread class calls the threading module and creates the threading. The subclass of thread to get the custom thread class.
#-*-Coding:utf-8-*-import threadingclass Th (threading. Thread): def __init__ (self, name): Threading. Thread.__init__ (self) self.t_name = name #调用父类构造函数 def run: #重写run () function, which is executed by default from this function Print "This is" + self.t_name if __name__ = = ' __main__ ': thread1 = Th ("thread_1") Thread1.start () #start () function Start the thread and execute the run () function automatically
Threading. An inheritable function of the thread class: GetName () Gets the thread object name SetName () sets the thread object name join () waits for the calling thread to end after running the command Setdaemon (BOOL) blocking mode, True: The parent thread does not wait for the child thread to end, F Alse Wait, the default is False Isdaemon () determines whether the child thread ends with the parent thread, that is, the value of the Setdaemon () setting isAlive () determines whether the thread is running the instance
Import Threadingimport Timeclass 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 () + "was over"
Join () Blocking wait
if __name__ = = ' __main__ ': thread1 = Th ("T1") Thread1.start () #thread1. Join () print "Main thread is Over
Without Thread1.join (), the following result is obtained: This is the thread T1 main thread is over 0 1 2 T1 are over do not wait for the thread1 to complete, after executing the statement. Add Thread1.join () to get the following result: This is the thread T1 0 1 2 T1 is over main thread was over blocking wait thread1 end before executing the following statement main thread wait
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 interpreter shutdown): That is, the main thread does not wait for the child thread to end. Multiple Child threads
if __name__ = = ' __main__ ': for I in range (3): t = Th (str (i)) T.start () print "Main thread was over"
Here t can handle multiple threads at the same time, i.e. T is the thread handle, and re-assignment does not affect the thread. It is strange here that when you run T.run (), no more threads are executed. Although it is unclear, use Start (). It is understood that start () is non-blocking parallel, while run is blocked. The Wire lock threading provides a thread lock, which enables threading synchronization.
Import Threadingimport Timeclass Th (threading. Thread): def __init__ (self, thread_name): Threading. Thread.__init__ (self) self.setname (thread_name) def run (self): threadlock.acquire () #获得锁之后再运行 print "This was thread" + self.getname () for I in range (3): time.sleep (1) print str (i) print SELF.G Etname () + "is Over" threadlock.release () #释放锁if __name__ = = ' __main__ ': threadlock = Threading. Lock () #设置全局锁 thread1 = th (' thread_1 ') thread2 = th (' thread_2 ') Thread1.start () Thread2.start ()
Get results: This is thread thread_1 0 1 2 thread_1 are over this is thread Thread_2 0 1 2 thread_2 are over original http://blog.csdn.ne t/ice110956/article/details/28421807
Python Threading Multithreaded Programming example