Non-blocking IPC using named pipes between Python and C++
Asynchronous communication with boost::asio
Suppose a program developed in Python needs to periodically share data with a program developed in C++. The requirement is that their communication shall be non-blocking. This means that the read process shall continue even if no new data is present.
According to https://en.wikipedia.org/wiki/named_pipe:
In computing, a named pipe (also known as a FIFO for its behavior) is an extension to the traditional pipe concept on Unix and Unix-like systems, and is one of the methods of inter-process communication (IPC). A named pipe makes use of the filesystem. It is explicitly created using
mkfifo()and two separate processes can access the pipe by name. One process can open it as a reader and the other as a writer.
In order to achieve non-blocking communication, the developer may create a named pipe at
/tmp/test. He or she writes data to this file in Python, and reads this data from C++. But if
std::iostream is used as a library, the developer is going to face a blocking read process, because this library does not support the concept of non-blocking I/O. But fortunately,
boost::asio does. According to the Boost.Asio documention, this is a cross-platform library for asynchronous network and low-level I/O programming. For my use case, it served as a nice solution.
The following code snippet implements an IPC read process in a non-blocking way:
You may use CMake 3.18 and Boost 1.74 to build the code:
Place both files in a new folder, and run following commands:
$ mkdir build
$ cd build
$ cmake ../
$ touch /tmp/test
The following screenshot confirms that
fifo listens for input on a named pipe, but does not block if no data is present:
The word Hello is decoded as an ASCII byte stream which ends with a LF or New Line character.
I am part of the systems engineering team for L4+ urban automated driving at Bosch, and personally responsible for sensor security. I research adversarial attack potentials on deep learning models and validate their robustness.
I have started this blog to post about projects that I run in my free time related to sensor security topics.
If you are interested to get in touch, please reach out to me on https://codefanatic.de