Application scenarios of the daemon
import time import random from multiprocessing import Process,JoinableQueue def consumer(name,q): while True: res = q.get() if res is None: break time.sleep(random.randint( 1,3 )) print ( ' \033[46mConsumer ==="%s ate %s\033[0m ' % (name,res)) q.task_done() def producer(name,q,food): for i in range(5): time.sleep (random.randint ( 1,2 )) res='%s%s' %(food,i) q.put(res) print ( ' \033[45m Producer ==="%s produced %s\033[0m ' % (name,res)) if __name__ == ' __main__ ' : # 1. Shared basin q= JoinableQueue() # 2. Producers p1=Process(target=producer,args=( ' egon ' ,q, ' baozi ' )) p2 =Process(target=producer,args=( ' Liu Qingzheng ' ,q, ' swill ' )) p3 =Process(target=producer,args=( ' Yang Jun ' ,q, ' rice ' )) # 3. Consumers c1=Process(target=consumer,args=( ' alex ' ,q)) c2=Process(target=consumer,args=('梁书东',q)) c1.daemon=True c2.daemon=True p1.start() p2.start() p3.start() c1.start() c2.start() #Make sure the producer has indeed finished producing p1.join() p2.join() p3.join() #After the producer completes production, get the total number of elements in the queue, and then the q.join() line of code will not run until the total number of elements becomes 0. q.join() # Once q.join() The end means that the queue is indeed emptied, and the consumer has indeed fetched all the data. print ( ' The main process ends ' )
from multiprocessing import JoinableQueue Using the JoinableQueue class, # q.task_done() tells the consumer that the queue has been fetched # # Make sure that the producer has indeed finished producing # p1.join() # p2.join() # p3.join() # # After the producer finishes producing, get the total number of elements in the queue, and then until The total number of elements becomes 0, and the line of code q.join() is completed . # q.join() # # #q.join() Once it ends, it means that the queue is indeed emptied, and the consumer has indeed put the data All cleaned up # print('The main process ends')
thread
what is thread
Thread refers to the working process of a pipeline
difference between process and thread
A process is not an execution unit at all. A process is actually a resource unit. A process has its own thread, and a thread is the execution unit.
Threads in the same process share resources within the process, and thread resources in different processes must be isolated.
The overhead of creating a thread is much less than the overhead of creating a process
There are two ways to start a thread:
from multiprocessing import Process from threading import Thread import time def task(name): print('%s is running' %name) time.sleep(3) if __name__ == '__main__': t = Thread(target=task,args=('ehon',)) t.start() print ( ' main thread ' )
from multiprocessing import Process from threading import Thread import time class MyThread(Thread): def run(self): print('%s is running' %self.name) time.sleep(3) if __name__ == '__main__': t =MyThread() t.start() print ( ' main thread ' )
Other methods of thread objects:
from multiprocessing import Process from threading import Thread,current_thread,active_count,enumerate import time def task(): print ( ' %s is running ' % current_thread().name) # current_thread(), returns the information of the current thread, here is the thread name time.sleep(3 ) if __name__ == '__main__': t1 = Thread(target=task,) t2 = Thread(target=task,) t3 = Thread(target=task,) t1.start() t2.start() t3.start() # print(t1.is_alive()) print (active_count()) #Returns the number of live threads, including the main thread print (enumerate()) #Returns all thread objects in the form of a list print ( ' main thread ' ,current_thread(). name)
Daemon thread:
The main thread is completely executed (all non-daemon threads end), and the daemon thread also ends
thread mutex
from threading import Lock
Change the thread local from concurrent to serial, the usage is the same as the process mutex
Sacrificing speed for guaranteed data correctness
Deadlocks and recursive locks
Recursive lock: For the same lock, every acquire, the lock count is incremented by one, and every release, the count is decremented by one
from threading import RLock mutexA=mutexB=RLock()
from threading import Thread,Lock,RLock import time mutexA=mutexB=RLock() class MyThread(Thread): def run(self): self.f1() self.f2() def f1(self): mutexA.acquire() print ( ' %s got A lock ' % self.name) mutexB.acquire() print ( ' %s got the B lock ' % self.name) mutexB.release() mutexA.release() def f2(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__': for i in range(10): t=MyThread() t.start() print ( ' main ' )
signal