Content Related:
Concept: Communication of processes
Queue: Create and use
Pipe: Creating and using
The concept of process communication
- The resource space of the process is independent of each other and is generally not accessible to each other. However, in many cases, processes need to communicate with each other to complete a function of the system. Processes coordinate their behavior by communicating with each other through the kernel and other processes.
- Communication method:
- Data transfer: One process sends its data to another process "like a socket, transfer data that needs to be communicated to each other"
- Pipeline: Use a separate area "not in the resource space of both sides", like a warehouse with two ports, the factory is responsible for the products in the East gate to the warehouse, the driver is responsible for pulling away products in ximen
- Resource sharing: The Convention is an area where both parties are free to pick and place
- Message Queuing: This is also a separate zone where sufficient permissions can add messages to the queue, and processes that are given Read permissions can read the messages in the queue
Queue:
- You can use the queue multiprocessing. Queue for process communication
Queue in multiprocessing module: from multiprocessing import Queue
- Use of queue:
- 1. Create object: Queue Object =queue ()
- 2. Incoming object: To use a queue object outside of the main process, you need to pass in as a parameter
- 3. Action object: "Get element: Queue object. Get ()", "Put element: Queue object. Put (Element)"
#queue in the multiprocessing fromMultiprocessingImportqueue,processdefF (q):#to be used outside of the main process, you need to pass in as a parameterQ.put (['HelloWorld']) defm (q):Print("get in P2:", Q.get ())if __name__=="__main__": Q=Queue () p=process (target=f,args=(q,)) P.start () P2=process (target=m,args=(q,)) P2.start ()
Pipe:
- You can use pipe pipes for process communication
Pipe in multiprocessing module: from multiprocessing import pipe
- Use of pipe:
- 1. Create object: The first pipe object, the second pipe object =pipe (), returns two objects, the first object can only be sent, the second object can only accept
- 2. Incoming object: The first pipe object is passed in the process to be sent, and a second pipe object is passed in the process to be received
- 3. Action object: "Get element: First object. recv ()", "put element: Second object." Send (Element) "
- 4. Close the pipeline when the operation is complete: the first object. Close (), first object. Close ()
fromMultiprocessingImportpipe,processdefF (conn): a=[1,2,3,4] Conn.send (a) conn.close ()defm (conn): a=conn.recv () conn.close ( )if __name__=="__main__": Parent_conn,child_conn=pipe ()#returns two values, the first one can only be sent, the second one can only receiveP1=process (target=f,args=(Child_conn,)) P2= Process (Target=m, args= (Parent_conn,))#P1.start () P2.start () P1.join () P2.join ( )
13.2, the process of communication: Queue, Pipe,