a daemon
The main process creates a daemon
One: the daemon will terminate after the main process code execution ends
Second: the child process can no longer be started in the daemon process, otherwise an exception will be thrown: AssertionError: daemonic processes are not allowed to have children
Note: The processes are independent of each other. The main process code runs and the daemon process terminates immediately.
from multiprocessing import Proscess
import time
def task(name):
print('%s is running'%name)
time.sleep(3)
if __name__=='__main__':
obj=Process(target=task,args=('lg',))
obj.deamon=True
obj.start()#Define the daemon process before sending a signal to the operating system to start
print('main')
2. Mutex lock:
from multiprocessing import Process,Lock
import time,random
Mutex:
Emphasis: It must be lock.acquire() once, and then lock.release() to release once, in order to continue lock.acquire(), not continuous lock.acpuire()
The difference between mutex vs join is:
Major premise: The principle of both is the same, both turn concurrency into serial, so as to ensure order
Difference: join is executed in an artificially specified order, while mutex locks are all processes competing equally, and whoever grabs it executes
Ticket grab system
from multiprocessing iimport Process,Lock
import json,os,time,random
mutex=Lock()
#The difference between mutex vs join is:
#mutex locks can make part of the code (code that modifies shared data) serialized, while join can only serialize the entire code
def check():
time.sleep (random.randint (1,3))
dic=json.load(open('a.json','r',encoding='utf-8'))
print('%s remaining votes%s'%(os.getpid(),dic['count']))
def get():
dic=json.load(open('a.json','r',encoding='utf-8'))
if dic['count']>0:
dic['count']-=1
json.dump('a.json','w',encoding='utf-8')
print('%s ticket purchase is successful'%os.getpid())
def task(lock):
check()
lock.acquire()
get()
lock.release
if __name__=='__main__':
for i in range(10):
p=Process(target=task,args=(mutex,))
p.start
Four IPC Communication Mechanisms
Communication between processes must find a medium that must satisfy
1, is shared by all processes
2, must be memory space
Additional Processing: Automatically handle lock issues
from multiprocessing import Queue
Alignment:
1. Shared space
2, is the memory space
3. Automatically help us deal with locking problems
q=Queue(3)
q.put('first')
q.put({'second':None})
q.put('三')
q.put(4)#blocking
print(q.get())
print(q.get())
print(q.get())
emphasize:
1. The queue is used to store messages for communication between processes, and the amount of data should not be too large
2. The memory limit exceeded by the value of maxxize becomes meaningless
The producer-consumer model
There are two important roles in this model:
1. Producer: The task responsible for creating data is likened to a producer
2. Consumers: Receive data created by producers for further processing, such characters are compared to consumers
Implementing the Three Elements of the Producer-Consumer Model
1. Producer
2. Consumers
3. Queue
When to use this model:
There are two obvious types of tasks in the program, one is responsible for production, and the other is responsible for processing production data.
Benefits of this model:
1. Realize the decoupling and decoupling of producers and consumers
2. Balanced productivity and consumption, that is, producers can keep producing and consumers can keep processing, because the two are no longer
communicate directly, but communicate with the queue