FiFoFile v1.0.0
A class that makes it easy to read and write lines in FiFo files (named pipes). This class was created to open a FiFo file and keep reading that file like a tail -f. Its use is not recommended for reading a single line.
A FiFo file does not generate I/O consumption or occupy disk space. Using a FiFo file, you can read tens of thousands of lines per second indefinitely. For sending logs to a FIFO file, we recommend using syslog-ng.
The FiFoFile class detects when syslog is restarted and automatically reopens the FiFo file, preventing the application from trying to read a file that has been closed and increasing CPU consumption.
Installation
pip install fifofile
Usage
Imagine a scenario where syslog-ng must send log lines to a FiFo file and you need to read those lines sent to that FiFo file.
- Create a FiFo file:
mkfifo /var/log/my_fifo_file.fifo
chmod 666 /var/log/my_fifo_file.fifo
-
Configure syslog-ng (rsyslog or syslog) to send a copy of your application's logs directly to this fifo file.
-
Use the FifoFile class to read these lines in real time
from fifofile import FiFoFile
fifo = FiFoFile('/var/log/my_fifo_file.fifo')
counter = 0
for line in fifo.readline():
counter += 1
print(line)
if counter == 50:
fifo.stop_reading() # is important to use this method to EXIT THE GENERATOR and CLOSE THE FIFO FILE.
print(">>>>>>> STOPPED AS REQUESTED")
break
or
from fifofile import FiFoFile
with FiFoFile('/var/log/my_fifo_file.fifo') as fifo: # When you exit this block, the FiFo file is closed.
counter = 0
try:
for line in fifo.readline(): # keep reading indefinitely
print(line) # do some cool stuff with the line
except:
fifo.stop_reading()
Methods
-
__init__(self, fifo_file_path:str, create_if_not_exists:bool=False, create_mode:str='0o666', polling_timeout:float=1.0)Constructor method for the FiFoFile class.
fifo_file_path(str): The path to the FIFO file.create_if_not_exists(bool, optional): If True, creates the FIFO file if it doesn't exist. Defaults to False.create_mode(str, optional): The file mode to use when creating the FIFO file. Defaults to '0o666'.polling_timeout(float, optional): The timeout value for polling the FIFO file. Defaults to 1.0.
-
create_fifo_file(fifo_file_path:str, create_mode:str='0o666', raise_if_exists:bool=False) -> boolStatic method to create a fifo file. You don't need to instantiate the class to use it.
fifo_file_path(str): The path to the FIFO file.create_mode(str, optional): The file mode to use when creating the FIFO file. Defaults to '0o666'.raise_if_exists(bool, optional): If True, raises an exception if the file already exists. Defaults to False.
-
is_fifo_file(fifo_file_path:str) -> boolStatic method to check if a file is a fifo file. You don't need to instantiate the class to use it.
fifo_file_path(str): The path to the file.
-
stop_reading()Method to stop the reading process. Important to use this method to EXIT THE GENERATOR and CLOSE THE FIFO FILE.
-
writeline(self, line:str, flush:bool=True)Method to write a line to the fifo file. This method opens the FiFo, writes the line, and closes the FiFo.
line(str): The line to write to the fifo file.flush(bool, optional): If True, flushes the fifo file after writing. Defaults to True.
-
read(self, size:int) -> Generator[Any, Any, Any]Method to read a line with a fixed size from the fifo file and return it as a generator.
size(int): The maximum number of characters to read.
-
readline(self, strip_line:bool=True) -> Generator[str, None, None]Method to read a line (until "\n") from the fifo file and return it as a generator.
strip_line(bool, optional): If True, strips leading and trailing whitespace from the line. Defaults to True.
-
__enter__(self)Method to support the
withstatement. -
__exit__(self, exc_type, exc_value, traceback)Method to support the
withstatement.
To Do List
- Add asyncio support
Sugestions, feedbacks, bugs...
Open an issue or e-mail me: ricardoabuchaim at gmail.com
Metadata
Release files for fifofile 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| fifofile-1.0.0.tar.gz | 6.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fifofile-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.9 kB
Release files / fifofile-1.0.0.tar.gz
| Download URL | fifofile-1.0.0.tar.gz |
|---|---|
| Size | 6.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
40c785339472e279512ff52bdec63b455fb66b46ba85cd0df27dc7f45f7b6789
|
|
BLAKE2b-256 checksum How to use checksums |
9ade930734c021eb0737d835a99a091d59a51932f92b245cbdbe8eeb285a296a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.0 CPython/3.11.9
|
Release files / fifofile-1.0.0-py3-none-any.whl
| Download URL | fifofile-1.0.0-py3-none-any.whl |
|---|---|
| Size | 6.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
06c74e0b5e4b834b7c38bba0f216637907d09d0ce40a4cd82dafb5b60182dd01
|
|
BLAKE2b-256 checksum How to use checksums |
bb16e9c98575ea922e509a3819309efd6b376558e6096b71ef9d8bbe5e9c2e24
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.0 CPython/3.11.9
|