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
How to create and use a named pipe in Python?
Named pipes, also known as FIFOs (First-In, First-Out), are special files that enable inter-process communication in Python. Unlike anonymous pipes limited to parent-child processes, named pipes allow unrelated processes to exchange data seamlessly.
Understanding Named Pipes
Named pipes are special files that exist in the file system but don't store data permanently. They act as conduits where data flows from one process to another through system memory. A key characteristic is that they must be opened as either read-only or write-only, not both simultaneously.
Creating a Named Pipe
Use the os.mkfifo() function to create a named pipe ?
import os
pipe_path = "/tmp/my_named_pipe"
try:
os.mkfifo(pipe_path)
print("Named pipe created successfully!")
except FileExistsError:
print("Named pipe already exists!")
except OSError as e:
print(f"Error creating named pipe: {e}")
Writing to a Named Pipe
Open the named pipe in write mode and use the write() method ?
import os
pipe_path = "/tmp/my_named_pipe"
# Writer process
try:
with open(pipe_path, "w") as pipe:
pipe.write("Hello from the writer process!")
pipe.flush() # Ensure data is sent immediately
print("Data written to named pipe")
except OSError as e:
print(f"Error writing to named pipe: {e}")
Reading from a Named Pipe
Open the named pipe in read mode to receive data ?
import os
pipe_path = "/tmp/my_named_pipe"
# Reader process
try:
with open(pipe_path, "r") as pipe:
data = pipe.read()
print(f"Received data: {data}")
except FileNotFoundError:
print("Named pipe does not exist!")
except OSError as e:
print(f"Error reading from named pipe: {e}")
Complete Example: Two-Process Communication
Here's a practical example showing how two separate processes can communicate ?
Writer Script
import os
import time
pipe_path = "/tmp/data_pipe"
# Create the named pipe
try:
os.mkfifo(pipe_path)
except FileExistsError:
pass # Pipe already exists
# Write data continuously
try:
with open(pipe_path, "w") as pipe:
for i in range(5):
message = f"Message {i+1} from writer"
pipe.write(message + "\n")
pipe.flush()
print(f"Sent: {message}")
time.sleep(1)
except OSError as e:
print(f"Writer error: {e}")
Reader Script
import os
pipe_path = "/tmp/data_pipe"
# Read data from the named pipe
try:
with open(pipe_path, "r") as pipe:
while True:
data = pipe.readline().strip()
if data:
print(f"Received: {data}")
else:
break
except OSError as e:
print(f"Reader error: {e}")
Cleaning Up Named Pipes
Always remove named pipes when finished to prevent resource leaks ?
import os
pipe_path = "/tmp/my_named_pipe"
try:
os.remove(pipe_path)
print("Named pipe removed successfully")
except FileNotFoundError:
print("Named pipe does not exist")
except OSError as e:
print(f"Error removing named pipe: {e}")
Best Practices
- Error handling: Always use try-except blocks when working with named pipes
- Permissions: Ensure proper file permissions for security
- Cleanup: Remove named pipes when no longer needed
- Blocking behavior: Remember that opening a named pipe for reading will block until a writer opens it
Conclusion
Named pipes provide a powerful mechanism for inter-process communication in Python. Use os.mkfifo() to create pipes, open them in separate processes for reading and writing, and always clean up resources when finished.
