Interprocess Communication
Process Pipes
A process is a basic unit of program execution with its own data and program components. Ordinary process address spaces are isolated, so processes cannot directly access each other's private data. They can use a pipe for interprocess I/O.
Creating a Process Communication Pipe
import multiprocessing
def send_data(pipe, data):
pipe.send(data)
print("[Process %d] Sent: %s" % (
multiprocessing.current_process().pid,
data
))
def recv_data(pipe):
print("[Process %d] Received: %s" % (
multiprocessing.current_process().pid,
pipe.recv()
))
def main():
send_end, recv_end = multiprocessing.Pipe()
sender = multiprocessing.Process(
target=send_data,
args=(send_end, "Hello!")
)
receiver = multiprocessing.Process(
target=recv_data,
args=(recv_end,)
)
sender.start()
receiver.start()
if __name__ == "__main__":
main()Output
[Process 4612] Sent: Hello!
[Process 6292] Received: Hello!Producer/Consumer Problem
Processes can send and receive data through a pipe. If much data is sent while the receiver handles it slowly, a queue provides buffering.
multiprocessing.Queue is the process-queue structure provided by Python for interprocess communication using FIFO ordering. Data from one producer is received in that producer's send order; messages from different producers may interleave.
Process Queue
import multiprocessing
import time
def produce(queue):
for item in range(3):
time.sleep(2)
data = "data-%d" % item
print("[%s] Produced: %s" % (
multiprocessing.current_process().name,
data
))
queue.put(data)
queue.put(None)
def consume(queue):
while True:
data = queue.get()
if data is None:
return
print("[%s] Consumed: %s" % (
multiprocessing.current_process().name,
data
))
def main():
queue = multiprocessing.Queue()
producer = multiprocessing.Process(
target=produce, name="Producer",
args=(queue,)
)
consumer = multiprocessing.Process(
target=consume, name="Consumer",
args=(queue,)
)
producer.start()
consumer.start()
if __name__ == "__main__":
main()Output
[Producer] Produced: data-0
[Consumer] Consumed: data-0
[Producer] Produced: data-1
[Consumer] Consumed: data-1
[Producer] Produced: data-2
[Consumer] Consumed: data-2A small shop: producer, warehouse, consumer
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
KNOWLEDGE QUIZ
Programming: an online shop's order-packing queue
Use Python multiprocessing to simulate incoming orders and a slower packing worker. Each order contains an ID, item name, and quantity.
- Create a producer process that puts six orders, ORD-001 through ORD-006, into a Queue with maxsize=3. Each order requests two notebooks.
- Create a consumer process that gets one order at a time, spends 0.2 seconds simulating packing, and prints the packed ID, item, and quantity. Let put/get block on a full/empty queue rather than polling.
- After the last order, send a None sentinel. Ensure all six orders are packed once in FIFO order, both child processes exit, and the main process waits for them and closes the queue.