NamedPipe

NamedPipe Class

class lib.namedpipe.NamedPipe(path)[source]

This module provides a class for getting command from a named pipe (FIFO).

Parameters

path (str) – absolute path where the FIFO is, or shall be created

Raises

TypeError – when path is not of type str

Create()[source]

Creates the FIFO file if if does not exist.

The access permissions are Read/Write for all (0666)

Returns

Nothing

Raises

OSError – Except when the FIFO already exists.

Delete()[source]

This function removes the named FIFO.

Parameters

pidfile (str) – Absolute path of a file to remove

Returns

Nothing

Exists()[source]

This method checks if the FIFO file for the named pipe exists. It not only checks the existence of the file but also if the file is a FIFO.

Returns

True When the FIFO exists (and is a valid FIFO), otherwise False

ReadLine()[source]

This method reads a line from the FIFO, if there is a line. If nothing got written to the FIFO, None gets returned. The line does not have a trailing \n.

This method is non-blocking

Example

pipe = NamedPipe("/tmp/test.fifo")

while True:
    line = pipe.ReadLine()
    if line == "refresh":
        UpdateCaches()

    time.sleep(1)

The file gets opened with O_NONBLOCK flag (non blocking read) and line buffering strategy.

Returns

A line from the pipe or None

WriteLine(line)[source]

Write a line into the named pipe. This method checks if the FIFO exists and rejects the line if not. If line is None or an empty string, nothing will be done.

This method is blocking!

Example

pipe = NamedPipe("/tmp/test.fifo")
pipe.WriteLine("refresh")
Parameters

line (str) – Line to write into the named pipe (Without \n!)

Returns

Nothing