Knowledge Point One:
Process: Resource Units
Thread: The execution unit of the CPU
Run of the process:
Open a process means to open a memory space, storage data, the resulting data lost
Running the thread:
The process of running the code is equivalent to running a thread
Auxiliary Understanding: One factory (operating system)--Not a workshop (start a process)--pipeline (thread) for each workshop
Knowledge Point two: 2 ways to turn on threads
fromThreadingImportThread#mode one: (referencing the thread class in the system) defTask (name):Print('%s is running') Print('%s is done') if __name__=='__main__': P=thread (target=task,args= ('Child Threads',)) P.start ()#Way Two: (Custom Class) classMythread (Thread):defRun (self):Print('%s is running'%self.name)Print('%s is done'%self.name)if __name__=='__main__': P=Mythread () P.start ( )
Knowledge Point three: Thread vs process
1th:1multiple process memory spaces are isolated from each other2The thread in the same process shares the data in the process 2nd: The thread is fast with the process (because there is no need to create a namespace, you do not need to copy the code of the parent class) fromThreadingImportThread#From multiprocessing import Process ImportTime x=100defTask (name):Globalx x=50Print('%s is running'%name) Time.sleep (3) Print('%s is done'%name)if __name__=='__main__': #P1=thread (target=task,args= (' child thread 1 ',)) #p1=process (target=task,args= (' child thread 1 ',)) #P1.start () #p.daemon=true #验证二: set P to Daemon, and it will wait until the other 2 processes except daemon p (p and main process print (' master ')) are finished. #P=thread (target=task,args= (' Child Threads ',)) #validation Three: threads are much faster than processes #p=process (target=task,args= (' child Thread ',)) #使用进程 #P.start () #会先打印print = (' master ')P=thread (target=task,args= ('Child Threads',))#Using ThreadsP.start ()#Print First: Child thread is running #Verify that a thread under the same process shares the data of the process class #print (x) #在主线程里面去调用x found x quilt thread has been re-modified and copied, all results are x=50 Print('Master')
Knowledge Point Four:
Knowledge Point four: Daemon threads
The daemon thread is waiting for the end of all internal threads to run (because the thread is controlled by the process,
If the same as the process just ends with the main thread end then other threads will also end up)
Properties for several threads:
1.current_thread (): Current_thread (). Name can get the name of the thread, similar to the Self.name command in the process
2.current_thread (). SetName can be the name of a thread
Knowledge Point five: mutex instance
The difference between a thread mutex and a process mutex, as long as a thread can share data from a process in a process
#lock cycle to a shared value minus 1 (the main function is to share part of the data, it is important to ensure that the user is a string to modify the value) fromThreadingImportThread,lockImportTimemutex=lock ()#thread Lock: 1. Everyone can share to (so put this position) 2. Lock let everyone go to the queue, run one, X minus 1x=100deftask ():Globalx mutex.acquire () Temp=x#if not locked, thread 1=100, thread 2=100, ... Time.sleep (0.1)#If you do not lock, the output will be 100, because when you wait 0.1 seconds, all the threads are already 100.X=temp-1mutex.release ()if __name__=='__main__': Start=time.time () t_l=[] #里面放的是thread1-thread100 forIinchRange (100): T=thread (target=Task) T_l.append (t) t.start ()#the line Cheng out in a very fast time . forTinchT_l:t.join ()#等待每个线程结束后运行下一步 Print('Master', x)#This step must be that all the threads are running out Print(Time.time ()-start)
Knowledge Point six: deadlock phenomenon and resolution deadlock scheme
fromThreadingImportThread,lock,rlockImport Time#Mutexa=lock () #死锁现象就是会存在当放当f1, F2 holding each other's locks, are stuck there to each other to unlock the phenomenon, causing the program has been stuck there#Mutexb=lock ()Mutexa=mutexb=rlock ()#changing deadlocks is the import of recursive locks: the function of the recursive lock is to be continuous acquire+1+1 ... plus lock, touch release-1-1 ... will automatically unlock it .classMyThread (Thread):defRun (self): Self.f1 () self.f2 ( )defF1 (self): Mutexa.acquire ()Print('%s got a lock.'%self.name) Mutexb.acquire ()Print('%s got the B lock.'%self.name) mutexb.release () mutexa.release ()defF2 (self): Mutexb.acquire ()Print('%s got the B lock.'%self.name) Time.sleep (0.1) Mutexa.acquire ()Print('%s got a lock.'%self.name) mutexa.release () mutexb.release ()if __name__=='__main__': forIinchRange (10): T=MyThread () T.start ( )#T1=mythread () #T1.start () # #T2=mythread () #T2.start () # #T3=mythread () #T3.start () Print('Master')
Knowledge Point Seven: semaphore
#From multiprocessing import Semaphore#Semaphore: Is the number of tasks that control concurrent execution at the same time Sm=semaphore (5), the following number is the semaphore#up to 5 people can be received at the same time, if out of 1 to unlock a ... Unlock up to 5 fromThreadingImportThread,semaphore,current_threadImporttime,randomsm=semaphore (5)#represents a semaphore of 5 and receives 5 values at oncedefGO_WC (): Sm.acquire ()Print('%s toilet ing'%Current_thread (). GetName ()) Time.sleep (Random.randint (1,3) ) sm.release ()if __name__=='__main__': forIinchRange (23): T=thread (target=GO_WC) T.start ()
Python open Thread in 2 ways, thread vs process (Guardian thread, mutex)