FIFOs are a very simple tool for communicating between processes. And using them in python is very easy. Simply call os.mkfifo(<path>) and treat the FIFO like any other file.

For example, we can create two simple python scripts, one for sending and one for receiving.

sender.py

import os

path = "/tmp/my_program.fifo"
os.mkfifo(path)

fifo = open(path, "w")
fifo.write("Message from the sender!\n")
fifo.close()

receiver.py

import os
import sys

path = "/tmp/my_program.fifo"
fifo = open(path, "r")
for line in fifo:
    print "Received: " + line,
fifo.close()

By running the programs in separate terminal windows, the receiver will print the following:
Received: Message from the sender!

  1. Kartoch says:

    I believe there is a small error: mkfifo must be in receiver, as this script needs to run first.

  2. Da says:

    What is the order to run?
    Why doesn’t restarting sender work?
    Must fifo be a file or could it also shared memory?

  3. forrest curo says:

    A fifo is like ‘shared memory’ in that it doesn’t get written into a disk file (unless circumstances force it into ‘virtual memory’) — It’s merely treated as if it were a file to make accessing it simpler for users.

    What I’m looking for: How an ongoing script should rest when waiting for new data to appear in the fifo, so as not to waste undue processor time, but respond quickly when new data does come in. Still looking.