Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to communicate between parent and child process using the pipe.
Inter-process communication (IPC) is essential when multiple processes need to share data and work together. In Python, the multiprocessing module provides various IPC mechanisms, one of which is the pipe.
A pipe allows data to be transferred between processes in either two-way (duplex) or one-way mode. It is particularly useful when a parent process creates a child process and they need to exchange messages or data.
Python multiprocessing.Pipe() Method
The multiprocessing.Pipe() method returns a pair of connection objects connected by a pipe. This pipe enables sending and receiving data between two processes.
Syntax
multiprocessing.Pipe(duplex=True)
Parameters
duplex: Boolean parameter that determines the pipe mode. When True (default), creates a duplex pipe allowing two-way communication. When False, creates a half-duplex pipe for one-way communication.
Parent to Child Communication
In this example, we send a string message from the parent process to the child process ?
import multiprocessing
def child_process(conn):
message = conn.recv()
print("Child received:", message)
if __name__ == "__main__":
parent_conn, child_conn = multiprocessing.Pipe()
# Create child process
child = multiprocessing.Process(target=child_process, args=(child_conn,))
child.start()
# Send message from parent to child
parent_conn.send("Welcome to TutorialsPoint")
child.join()
Child received: Welcome to TutorialsPoint
Bidirectional Communication
Here both parent and child processes exchange messages using a duplex pipe ?
import multiprocessing
def child_process(conn):
# Child sends first message
conn.send("Have a nice day!")
# Child receives response from parent
response = conn.recv()
print("Child received:", response)
if __name__ == "__main__":
parent_conn, child_conn = multiprocessing.Pipe(duplex=True)
# Create child process
child = multiprocessing.Process(target=child_process, args=(child_conn,))
child.start()
# Parent receives message from child
message = parent_conn.recv()
print("Parent received:", message)
# Parent sends response back to child
parent_conn.send("Same to you!")
child.join()
Parent received: Have a nice day! Child received: Same to you!
Key Points
- Pipe returns two connection objects: one for each end of the pipe
- Use
send()to transmit data andrecv()to receive data - Duplex pipes allow bidirectional communication
- Always call
join()to wait for child processes to complete - Pipes are faster than queues for simple two-process communication
Conclusion
Python's multiprocessing.Pipe() provides an efficient way to establish communication between parent and child processes. Use duplex pipes for bidirectional communication or half-duplex for one-way data flow.
