Overview
I'm trying to take in raw data from CAVA, https://github.com/karlstav/cava which is a terminal based audio visualizer that gives the option to output the bar values to a FIFO named pipe. My ultimate goal is to take those values into a Python program and manipulate them to display on an LED Matrix. While there are answers to this specific issue on Windows, I'm working on a Raspberry Pi running Raspbian and am looking for linux based answers.
The problem
I have the perfect code I need working in C, but I cannot get the same logic to work in Python. Here's the C code:
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int fd1;
// FIFO file path
char * myfifo = "/home/pi/aproj/audio/outs/theout";
// Creating the named file(FIFO)
// mkfifo(<pathname>,<permission>)
mkfifo(myfifo, 0666);
char str1[80], str2[80];
while (1)
{
// First open in read only and read
fd1 = open(myfifo,O_RDONLY);
read(fd1, str1, 80);
// Print the read string and close
printf("User1: %s\n", str1);
printf("this is line by line\n");
close(fd1);
}
return 0;
}
When I try to make to jump to Python, multiple parts fail and I haven't been able to find solutions online. For one, trying to make the fifo file using:
import os
thepath="/home/pi/aproj/audio/outs/theout"
os.mkfifo(thepath)
gives the error: FileExistsError: [Errno 17] File exists. This makes sense to a degree because CAVA makes the FIFO, but why does the logic work in C?
Even if I skip the mkfifo step, Python then gets hung up on opening the FIFO:
import os
thepath="/home/pi/aproj/audio/outs/theout"
#os.mkfifo(thepath)
while True:
fd1 = open(thepath,'r');
print('hello')
^this code gets stuck and never prints hello
How can I open a named pipe in Python and read it in line by line as the data is being produced? Again, the C code does exactly what I need, I just need help getting that logic to work in Python (3.7.3). Thanks in advance for the help!