I. What is a process
The process is actually a resource unit, and the thread inside the process is the executing unit on the CPU, and the thread actually refers to the code execution process.
Ii. the difference between a process and a thread
1. Multiple threads under the same process share resources within the process
2. The cost of creating a thread is much smaller than the process
Three ways to use the thread class to open threads
1. Thread class
methods for thread instance objects # isAlive (): Returns whether the thread is active. # getName (): Returns the thread name. # setName (): Sets the thread name. Some of the methods provided by the threading module are:# threading.currentthread (): Returns the current thread variable. # threading.enumerate (): Returns a list that contains 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 ()).
2. Open Thread
fromMultiprocessingImportThreadImport TimedefTask (name):Print('%s is running'%name) Time.sleep (2) Print('%s is done'%name)if __name__=='__main__': T= Thread (Target=task, args= ('Thread 1',)) T.start ()Print('Master')
Way One
classMythread (Thread):defRun (self):Print('%s is running'%Self.name, Current_thread (). Name) Time.sleep (2) Print('%s is done'%Self.name, Current_thread (). Name)if __name__=='__main__': T=Mythread () T.start ( )Print('Master', Active_count (), Current_thread (). Name)
Mode two
Iv. Unified Process resource sharing between threads
Multiple threads under the same process share resources within the process
from Import =def task ():Global n = 0if__ name__'__main__': = Thread (target=Task) T.start () t.join () print(n)# 0
inter-thread resource sharing under the unified process
Five, The Guardian thread
Whether it is a process or a thread, follow: Guardian xxx will wait for the main xxx to be destroyed after the completion of the operation
★ The operation is finished not to terminate the operation
# 1. For the main process, running complete means that the main process code is running # 2. To the main thread said, run complete refers to the main thread in the process of all non-daemon threads run complete, the main thread is run complete
# 1 The main process is finished after its code is finished (the daemon is recycled at this point), and then the main process will wait until the non-daemon child processes have finished running to reclaim the child process's resources (otherwise it will produce a zombie process), will be ended, # 2 The main thread runs after the other non-daemons have finished running (the daemon thread is recycled at this point). Because the end of the main thread means the end of the process, the resources of the process as a whole are recycled, and the process must ensure that the non-daemon threads are finished before they end.
fromThreadingImportThreadImport TimedefTask (name):Print('%s is running'%name) Time.sleep (2) Print('%s is done'%name)if __name__=='__main__': T= Thread (Target=task, args= ('Thread 1',)) T.daemon=True T.start ()Print('Master')
Daemon Process
Six, mutual exclusion lock
fromThreadingImportThread,lockImportTimemutex=lock ()#The thread is a shared resource, so you don't need to pass the lock as a parametern=100deftask ():Globaln mutex.acquire () temp=N Time.sleep (0.1) n=temp-1mutex.release ()if __name__=='__main__': t_l=[] forIinchRange (100): T=thread (target=Task) T_l.append (t) t.start () forTinchT_l:t.join ()Print(n)
Mutual exclusion Lock
Concurrent Programming (iv)--process