I have been trying to read continuous data from a named pipe. But for some reason if I don't put a delay, the receiver will just stop reading and only a blank screen is shown after a few samples.
I need to send continuous data that might change in milliseconds, so that's why putting a delay wouldn't work. I am trying to simulate it first using a while loop (the real script will be reading financial data). This is my first attempt:
This is the sender, a python script:
import os
import time
try:
os.remove("/tmp/pipe7") # delete
except:
print "Pipe already exists"
os.mkfifo("/tmp/pipe7") # Create pipe
x = 0
while True:
x = time.time()
pipe = open("/tmp/pipe7", "w")
line = str(x) + "\r\n\0"
pipe.write(line)
pipe.close()
#time.sleep(1)
os.remove("/tmp/pipe7") # delete
This is the receiver in C/C++:
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <iostream>
#include <sys/stat.h>
#define MAX_BUF 1024
using namespace std;
int main()
{
while(1){
char buf[MAX_BUF];
memset (buf, 0, sizeof(buf)); //Clearing the message buffer
int fd = open("/tmp/pipe7", O_RDONLY); // Open the pipe
read(fd, buf, MAX_BUF); // Read the message - unblock the writing process
cout << buf << endl;
close(fd); // Close the pipe
}
return 0;
}
What's wrong with my approach? And what's the best way to communicate continuously between the two programs using pipes?