linux named pipes for ipc

I recently built a simple scripted daemon fo running a command indefinitely, periodically checking the output, and sending passive check results to icinga in the event of any problems found (omping is the command in question in case that matters =).  I was looking into several solutions for running this process asynchronously and polling the output in a non-blocking way, and I ran into a couple of snags along the way.

My first go-to for this sort of thing is typically the Popen method found within the subprocess module in Python.  However, while you can asynchronously spawn new processes and poll() them for completion, there seems to be no way to read the process’ stdout without blocking until the process completes (or receives a SIGKILL/SIGTERM or otherwise dies).  My quest led me to Twisted, a powerful (and somewhat intimidating) framework for asynchronous event-driven programming.

After working with subprocesses in Twisted for a bit, I realized that there had to be an easier way to solve this somewhat straightforward use case.  Then I figured, why not use a named pipe?  Trivial to implement one-way inter-process communication.  If it works for icinga with thousands of passive checkresults submitted per second, it has to work here too.

A simple fork() (one instance to run the command and write to the pipe, and another to read said pipe and handle output) and everything worked perfectly.  Huzzah!  Score another victory for the unix way?  =D

Leave a comment