This article is an example of Python process communication Queue, Pipe. shared for your reference, as follows:
Content Related:
Concept: communication of processes
Queue: Creation and Use
Pipe: Creation and Use
Concept of process communication
- The resource spaces of processes are independent of each other and are generally not accessible to each other. However, in many cases processes need to communicate with each other to accomplish a function of the system. Processes coordinate their behavior by communicating with the kernel and other processes.
- Methods of communication:
- Data transfer: one process sends its data to another process [like a socket, which transfers data that needs to be communicated to the other party].
- Pipeline: using a separate area [not in the resource space of both parties], like a warehouse with two openings, the manufacturer is responsible for putting the product in the warehouse at the east gate, and the driver is responsible for pulling the product at the west gate
- Resource sharing: agree on an area where both parties can pick up and drop off as they please
- Message Queue: This is also a separate area where processes with sufficient privileges can add messages to the queue, and processes that have been given read access can read away messages from the queue.
Queue:
- You can use queues for process communication
- Data transfer: one process sends its data to another process [like a socket, which transfers data that needs to be communicated to the other party].
- Pipeline: using a separate area [not in the resource space of both parties], like a warehouse with two openings, the manufacturer is responsible for putting the product in the warehouse at the east gate, and the driver is responsible for pulling the product at the west gate
- Resource sharing: agree on an area where both parties can pick up and drop off as they please
- Message Queue: This is also a separate area where processes with sufficient privileges can add messages to the queue, and processes that have been given read access can read away messages from the queue.
- You can use queues for process communication
Queue existmultiprocessing modules:from multiprocessing import Queue
- Queue usage:
- 1. Create object: Queue object = Queue ()
- 2. Incoming Objects: To use a Queue object outside the main process, you need to pass it in as a parameter
- 3. Operation Objects: [Get Element: Queue Object.get()], [Put Element: Queue Object.put(Element)]
#Queue in multiprocessing from multiprocessing import Queue,Process def f(q):# To be used outside of the main process, it needs to be passed as a parameter to the (['helloworld']) def m(q): print("get in p2:",()) if __name__=="__main__": q=Queue() p=Process(target=f,args=(q,)) () p2=Process(target=m,args=(q,)) ()
Pipe:
- You can use the pipe Pipe for process communication
Pipe existmultiprocessing modules:from multiprocessing import Pipe
- Pipe usage:
- 1. Create objects: the first Pipe object, the second Pipe object = Pipe (), return two objects, the first object can only send, the second object can only receive
- 2. Incoming objects: in the process to send, pass the first Pipe object; in the process to receive, pass the second Pipe object
- 3. Operation object: [get element: the first object.recv()], [put element: the second object.send(element)]
- 4. After the operation is completed to close the pipeline: the first object.close(), the first object.close()
from multiprocessing import Pipe,Process def f(conn): a=[1,2,3,4] (a) () def m(conn): a=() () if __name__=="__main__": parent_conn,child_conn=Pipe()# Returns two values, the first can only be sent and the second can only be received p1=Process(target=f,args=(child_conn,)) p2 = Process(target=m, args=(parent_conn,))# () () () ()
Readers interested in more Python related content can check out this site's topic: theSummary of Python process and thread manipulation techniques》、《Python Data Structures and Algorithms Tutorial》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniques》、《Python introductory and advanced classic tutorials》、《Python + MySQL Database Programming Tutorial for Beginnersand theSummary of common database manipulation techniques in Python》
I hope that what I have said in this article will help you in Python programming.