Python 3 – os.mkfifo() Method
In Python, the os
module provides a method called mkfifo()
that is used to create named pipes (FIFOs) on Unix-based systems. A named pipe is a mechanism for inter-process communication (IPC) that allows two or more processes to communicate with each other through a pseudofile. In this article, we will explore the os.mkfifo()
method in more detail and see how it can be used in Python programming.
Syntax
The syntax for using the os.mkfifo()
method is as follows:
os.mkfifo(path, mode=0o666, *, dir_fd=None)
Here, path
is the name of the FIFO, and mode
is an optional parameter that specifies the permissions to be applied to the FIFO. The default permissions are 0o666
, which means that any user can read from or write to the pipe. The dir_fd
parameter can be used to specify a file descriptor for the directory containing the FIFO.
Example
Let’s look at a simple example of how to use the os.mkfifo()
method to create a named pipe:
import os
fifo_path = '/tmp/my_fifo'
if not os.path.exists(fifo_path):
os.mkfifo(fifo_path)
print(f"Named pipe created: {fifo_path}")
else:
print(f"Named pipe already exists: {fifo_path}")
In this example, we first define the path of the named pipe we want to create (/tmp/my_fifo
). We then check to see if a file or directory exists at that path using the os.path.exists()
method. If no such file or directory exists, we use the os.mkfifo()
method to create a named pipe at that path with default permissions (0o666
). Finally, we print a message to indicate whether the named pipe was successfully created or already existed.
Using a Named Pipe for Inter-Process Communication
Once a named pipe has been created using the os.mkfifo()
method, we can use it for inter-process communication (IPC) between two or more processes. In the following example, we will create two separate Python scripts that communicate with each other using a named pipe.
Script 1: Writing to a Named Pipe
The first script will write a message to the named pipe once per second:
import os
import time
fifo_path = '/tmp/my_fifo'
if not os.path.exists(fifo_path):
os.mkfifo(fifo_path)
with open(fifo_path, 'w') as fifo:
while True:
message = f"Hello from Script 1 at {time.time()}\n"
fifo.write(message)
fifo.flush()
time.sleep(1)
In this script, we first create the named pipe if it does not already exist. We then open the named pipe in write mode ('w'
) using the open()
function and a with
statement. This allows us to automatically close the file when we are done writing to it.
Inside the loop, we create a message string that includes the current timestamp using the time.time()
function. We then write the message to the named pipe using the write()
method and flush the buffer using the flush()
method.
Finally, we pause for one second using the time.sleep()
method before starting the loop again.
Script 2: Reading from a Named Pipe
The second script will read messages from the named pipe that are written by the first script and print them to the console:
import os
fifo_path = '/tmp/my_fifo'
if not os.path.exists(fifo_path):
os.mkfifo(fifo_path)
with open(fifo_path, 'r') as fifo:
while True:
message = fifo.readline()
print(f"Received message: {message}", end='')
In this script, we perform the same initial checks to ensure that the named pipe exists before proceeding.
We then open the named pipe in read mode ('r'
) using the open()
function and a with
statement. Inside the loop, we use the readline()
method to read the next message from the named pipe. We then print the message to the console using the print()
function.
Conclusion
The os.mkfifo()
method provides a simple way to create named pipes in Python, enabling inter-process communication between separate processes running on the same machine. By using the open()
function to read from or write to a named pipe, we can build more complex systems that rely on IPC for communication. When combined with other Python libraries, such as multiprocessing
or subprocess
, this technique can enable sophisticated parallel processing and automation workflows.